• 3196阅读
  • 1回复

[提问]在读写文件的时候C++调用QML窗口失效 [复制链接]

上一主题 下一主题
离线robin-zf
 

只看楼主 倒序阅读 楼主  发表于: 2017-08-30
在读写文件的时候C++调用QML窗口失效
写了一个程序,C++里实现业务把N个文件的内容读出来,然后把内容写到另一个文件。读写文件时想调用一个进度条窗口显示读到第几个文件。可是运行程序时,进度窗口显示的内容就是不出来,一定要等到读完写完,它才显示进度条。Wprocess.qml:
  1. import QtQuick 2.0
  2. import QtQuick.Controls 2.0
  3. import QtQuick.Controls 1.4 as Control
  4. import QtQuick.Controls.Styles 1.4
  5. import QtGraphicalEffects 1.0
  6. ApplicationWindow{
  7.     id:wprocess
  8.     width: 400;
  9.     height: 100;
  10.     visible: true
  11.     Text {
  12.         id:dir1
  13.         objectName: "text1"
  14.         text: "正在读取文件夹"
  15.         font.pixelSize: 12
  16.         anchors.top:parent.top
  17.         anchors.topMargin: 10
  18.         anchors.left: parent.left
  19.         anchors.leftMargin: 4
  20.     }
  21.     Control.ProgressBar{
  22.         id:processbar1
  23.         objectName: "bar1"
  24.         minimumValue: 0
  25.         maximumValue: 100
  26.         value: 0
  27.         width: 390
  28.         height: 20
  29.         anchors.top:dir1.bottom
  30.         anchors.topMargin: 4
  31.         anchors.left: parent.left
  32.         anchors.leftMargin: 2
  33.             }
  34.   }

调用代码:
  1. int ModelPage1::copybydate(const QString &read,const QString &write,QString date){
  2.     FileHelper* filehelper=new FileHelper;
  3.     QFileInfoList list=filehelper->getFiles(read);//获取目录下的子目录
  4.     QString str1,str2;
  5.     //--文件存在,删除
  6.     str2=write+"/"+date;
  7.     QFile file(str2);
  8.     if(file.exists()){
  9.         file.remove();
  10.     }
  11.     //--
  12.     QQmlEngine engine;
  13.     QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///Wprocess.qml")));
  14.     QObject *object = component.create();
  15.     QObject *text1 = object->findChild<QObject*>("text1");
  16.     QObject *bar1 = object->findChild<QObject*>("bar1");
  17.     if(bar1) {
  18.         bar1->setProperty("maximumValue", list.length());
  19.     }
  20.     int barvalue=1;
  21.     for(int i=0;i<list.length();i++)  {//start to iterator file
  22.         QFileInfo fileinfo=list.at(i);
  23.         if(fileinfo.fileName()=="." or fileinfo.fileName()==".."){
  24.             i++;
  25.             continue;
  26.         }
  27.         date=date.replace("-","");
  28.         str1=fileinfo.path() + "/" + fileinfo.fileName() + "/" + date + ".ejl";
  29.         if(text1) {
  30.             text1->setProperty("text", "正在读取文件"+str1);
  31.         }
  32.         filehelper->copyAdd(str1,str2);//增量写文件
  33.         barvalue=barvalue+1;
  34.         bar1->setProperty("value",barvalue);
  35.     }
  36.     return 0;
  37. }

读写中时这样:
读完之后,就显示正常了

求大神有没有解决办法!!!
离线never_forget

只看该作者 1楼 发表于: 2017-08-31
哪有你这样玩儿的,干嘛要这么写啊,这个当然是加载 QML 文件,然后注册 C++ 类到 QML 上下文,然后绑定信号,刷新界面啊。你这个写法是哪找来的,这个在读取文件的时候可能阻塞了进程,所以 QML 还不能显示了,文件操作完成了才显示界面!
快速回复
限100 字节
 
上一个 下一个