robin-zf |
2017-08-30 11:16 |
在读写文件的时候C++调用QML窗口失效
写了一个程序,C++里实现业务把N个文件的内容读出来,然后把内容写到另一个文件。读写文件时想调用一个进度条窗口,显示读到第几个文件。可是运行程序时,进度窗口显示的内容就是不出来,一定要等到读完写完,它才显示进度条。Wprocess.qml:- import QtQuick 2.0
import QtQuick.Controls 2.0 import QtQuick.Controls 1.4 as Control import QtQuick.Controls.Styles 1.4 import QtGraphicalEffects 1.0 ApplicationWindow{ id:wprocess width: 400; height: 100; visible: true Text { id:dir1 objectName: "text1" text: "正在读取文件夹" font.pixelSize: 12 anchors.top:parent.top anchors.topMargin: 10 anchors.left: parent.left anchors.leftMargin: 4 }
Control.ProgressBar{ id:processbar1 objectName: "bar1" minimumValue: 0 maximumValue: 100 value: 0 width: 390 height: 20 anchors.top:dir1.bottom anchors.topMargin: 4 anchors.left: parent.left anchors.leftMargin: 2 } }
调用代码:
- int ModelPage1::copybydate(const QString &read,const QString &write,QString date){
FileHelper* filehelper=new FileHelper; QFileInfoList list=filehelper->getFiles(read);//获取目录下的子目录 QString str1,str2; //--文件存在,删除 str2=write+"/"+date; QFile file(str2); if(file.exists()){ file.remove(); } //-- QQmlEngine engine; QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///Wprocess.qml"))); QObject *object = component.create(); QObject *text1 = object->findChild<QObject*>("text1"); QObject *bar1 = object->findChild<QObject*>("bar1"); if(bar1) { bar1->setProperty("maximumValue", list.length()); } int barvalue=1; for(int i=0;i<list.length();i++) {//start to iterator file QFileInfo fileinfo=list.at(i); if(fileinfo.fileName()=="." or fileinfo.fileName()==".."){ i++; continue; } date=date.replace("-",""); str1=fileinfo.path() + "/" + fileinfo.fileName() + "/" + date + ".ejl"; if(text1) { text1->setProperty("text", "正在读取文件"+str1); } filehelper->copyAdd(str1,str2);//增量写文件 barvalue=barvalue+1; bar1->setProperty("value",barvalue); } return 0; }
读写中时这样:[attachment=17769] 读完之后,就显示正常了 [attachment=17770] 求大神有没有解决办法!!! |
|