查看完整版本: [-- 在读写文件的时候C++调用QML窗口失效 --]

QTCN开发网 -> Qt基础编程 -> 在读写文件的时候C++调用QML窗口失效 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

robin-zf 2017-08-30 11:16

在读写文件的时候C++调用QML窗口失效

写了一个程序,C++里实现业务把N个文件的内容读出来,然后把内容写到另一个文件。读写文件时想调用一个进度条窗口,显示读到第几个文件。可是运行程序时,进度窗口显示的内容就是不出来,一定要等到读完写完,它才显示进度条。Wprocess.qml:
  1. 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
                }
      }

调用代码:
  1. 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]
求大神有没有解决办法!!!

never_forget 2017-08-31 08:34
哪有你这样玩儿的,干嘛要这么写啊,这个当然是加载 QML 文件,然后注册 C++ 类到 QML 上下文,然后绑定信号,刷新界面啊。你这个写法是哪找来的,这个在读取文件的时候可能阻塞了进程,所以 QML 还不能显示了,文件操作完成了才显示界面!


查看完整版本: [-- 在读写文件的时候C++调用QML窗口失效 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled