c++ - 使用qml + QQuickView作为初始屏幕不起作用

标签 c++ qt qml

我想在我的c++ / qt / qml应用程序中添加一个初始屏幕。我添加了以下代码:

int main(int argc, char *argv[])
{


    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

    QQuickView *view = new QQuickView;
    view->setSource(QUrl(QLatin1String("qrc:/Splash.qml")));
    view->show();


   engine.load(QUrl(QLatin1String("qrc:/main.qml")));

   view->close();
   app.exec();

   return 0;

}

和Splash.qml文件:
import QtQuick 2.0
import QtQuick.Controls 2.1

Item {
    visible: true
    width: 640
    height: 480
    BusyIndicator {
        anchors.centerIn: parent
        width: 100
        height: 100
        running: true
        Component.onCompleted: {
            console.log("splash screen... " + x + " " + y + " " + width + "x" + height)
            visible=true
        }
    }
}

当应用程序启动时,消息:“qml:启动屏幕... 270 190 100x100”被打印,但是QQuickWindow只是白色。 main.qml的主窗口可以正常工作。我试图在Splash.qml中加载图像,但存在同样的问题。而且,我不能使用QQuickView::setWindowFlags使其成为无框架窗口。我正在使用win7 64位操作系统。

最佳答案

您可以在QML中轻松做到这一点:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Window 2.0

Window {
    id: mainWindow
    width: 600
    height: 400
    visible: false
    x: Screen.width / 2 - width / 2;
    y: Screen.height / 2 - height / 2;

    Window {
        id: splashWindow
        width: 300
        height: 200
        visible: true
        x: Screen.width / 2 - width / 2;
        y: Screen.height / 2 - height / 2;
        flags: Qt.SplashScreen;
        Rectangle {
            anchors.fill: parent
            color: "green";
        }

        ProgressBar {
            id: progressBar
            anchors.centerIn: parent
            anchors.margins: 20
            value: 0.01
        }

        Timer {
            id: timer
            interval: 50
            repeat: true
            running: true
            onTriggered: {
                progressBar.value += 0.01
                if(progressBar.value >= 1.0) {
                    timer.stop();
                    mainWindow.visible = true;
                    splashWindow.destroy();
                }
            }
        }
    }
}

关于c++ - 使用qml + QQuickView作为初始屏幕不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44214812/

相关文章:

c++ - 如果 QThread 的完成信号连接到 deleteLater,是否有必要删除在 QThread 上运行的对象?

c++ - ComboBox初始化错误 : Cannot read property 'constructor' of undefined

c++ - 加载 Rcpp 并运行示例代码

c++ - 将项目添加到列表控件时 UI 卡住

qt - 如何在失去焦点时隐藏 QML 窗口

c++ - 每个条目有 2 列的 QTreeView

c++ - 如何在 Qt 的列表列表中推送值?

qt - qt qml 和 qt Quick 的区别

c++ - 在字符串中为每个第N个字符插入空格的函数无法正常工作吗?

c# - 无法添加对 WinRT C++ DLL 项目的引用