【问题标题】:How to show the QML window before the program is completed?如何在程序完成之前显示 QML 窗口?
【发布时间】:2015-10-12 06:23:20
【问题描述】:

问题很简单:window 不渲染和刷新,直到程序完成。它只是没有显示任何东西。 即使长周期还没有完成,我也想看到窗口。 如果有任何帮助,我将不胜感激!

#include <QtGui>
#include <QtQml>

int main(int _nArgCount, char * _pArgValues[]) {
    QApplication app(_nArgCount, _pArgValues);

    //QMLblock
    QString strQmlPath = "../main.qml";
    QQmlApplicationEngine engine;
    QQmlComponent pComponent(&engine, strQmlPath);
    if( pComponent.status()==QQmlComponent::Error )
           { qDebug()<<"Error:"<<pComponent.errorString();
              return app.exec();
           }
    QObject * pQmlObject = pComponent.create();

    QObject * pWindow = pQmlObject->findChild<QObject*>("initStateGui");
    QObject * pWindowNext = pQmlObject->findChild<QObject*>("searchRemovableGui");
    pWindow->setProperty("visible","false");
    pWindowNext->setProperty("visible","true");
    QObject * pList = pQmlObject->findChild<QObject*>("devicesList");
    QStringList s;
    QString str;

    s.append("3");
    pList->setProperty("model",s);

        for (int i=0; i<5; i++){
        s.append(str.number(i));
        pList->setProperty("model",s);

    }


   return app.exec();

}

还有我的 QML(我认为不需要,但无论如何):

import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Window 2.2

QtObject {
    property real defaultSpacing: 10
    property SystemPalette palette: SystemPalette { }

    property var controlWindow: Window {
        width: 500
        height: 500
        color: palette.window
        title: "Updater"
        visible: true
        //init state
        Column {
            id: initStateGui
            objectName: "initStateGui"
            anchors.fill: parent
            anchors.margins: defaultSpacing
            spacing: defaultSpacing
            property real cellWidth: initStateGui.width / 3 - spacing
            visible: true
            Text { text: "Init state" }
            Grid {
                id: grid
                columns: 3
                spacing: defaultSpacing
                width: parent.width
                Button {
                    id: showButton
                    width: initStateGui.cellWidth
                    text: "Cancel"
                    onClicked: Qt.quit()
                }

                Button {
                    id: initStateContinue
                    objectName: "initStateContinue"
                    width: initStateGui.cellWidth
                    text: "Continue"
                    signal sigInitStateContinue()
                    onClicked: initStateContinue.sigInitStateContinue()

                }

            }

            Text {
                id: textLabel
                text: "Welcome to the updater!"
            }
            Rectangle {
                id: horizontalRule
                color: "black"
                width: parent.width
                height: 1
            }

        }

        //updater update state
        Column {
            id: updaterUpdateGui
            objectName: "updaterUpdateGui"
            anchors.fill: parent
            anchors.margins: defaultSpacing
            spacing: defaultSpacing
            visible: false
            property real cellWidth: initStateGui.width / 3 - spacing
            Text { text: "UpdaterUpdate State" }
            Grid {
                id: grid1
                columns: 3
                spacing: defaultSpacing
                width: parent.width
                Button {
                    id: showButton1
                    width: initStateGui.cellWidth
                    text: "Cancel"
                    onClicked: Qt.quit()
                }
                Button {
                    id: updaterUpdateContinue
                    objectName: "updaterUpdateContinue"
                    width: initStateGui.cellWidth
                    text: "Continue"
                    signal sigUpdaterUpdateContinue()
                    onClicked: updaterUpdateContinue.sigUpdaterUpdateContinue()

                }

            }

            Text {
                text: "Update is started!"
            }
            Rectangle {
                id: horizontalRule1
                color: "black"
                width: parent.width
                height: 1
            }

        }

        //removable Search gui
        Column {
            id:searchRemovableGui
            objectName: "searchRemovableGui"
            anchors.fill: parent
            anchors.margins: defaultSpacing
            spacing: defaultSpacing
            visible: false
            property real cellWidth: initStateGui.width / 3 - spacing
            Text { text: "Removable search State" }
            Grid {
                id: grid2
                columns: 3
                spacing: defaultSpacing
                width: parent.width
                Button {
                    id: showButton2
                    width: initStateGui.cellWidth
                    text: "Cancel"
                    onClicked: Qt.quit()
                }
            }

            Text {
                text: "Searching for removable, please wait...!"
            }




            ListView {
                id:devicesList
                objectName:"devicesList"
                width: 100; height: 500
                model: myModel
                delegate: Rectangle {
                    height: 15
                    width: 100
                    Text { text: modelData }


                }
            }

        }
    }
}

补充:我不需要线程,我需要看到带有标题的冻结窗口。

如果我添加按钮,我会看到它,并在按下按钮后开始循环。 没有按钮,窗口不会呈现,我也找不到怎么做。

【问题讨论】:

  • QML 是必需的。顺便说一句,您的应用程序中有错误:main.qml:141: ReferenceError: myModel is not defined
  • 你是什么意思 - QML 是必要的?我没有使用它吗? (请不要注意关于模型的警告)
  • 你说i don't think it's needed, but anyway,我只是说这是关于QML的问题,所以看到一些QML很好。
  • 我查看了您的应用程序,它立即为我加载。我没有看到问题。
  • 嗯,问题变得神秘......所以如果你在循环的开始处放置一个断点 - 你可以看到带有列表视图的窗口(以及唯一的元素 - “3” )?

标签: qt window refresh qml qtquick2


【解决方案1】:

在一个线程中实现是不可能的。 只有将长进程移动到另一个线程才允许渲染 GUI。

【讨论】:

    猜你喜欢
    • 2018-11-04
    • 2019-02-07
    • 2013-11-04
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多