Qt QQmlApplicationEngine refuse to display qml cod

2019-06-28 01:29发布

I have this pro file

QT += qml quick widgets
TARGET = sPassKeeper
include(src/src.pri)
include(qml/qml.pri)
RESOURCES += \
    resources.qrc

in src pri and qml pri only single lines qml.pri

OTHER_FILES += \
    qml/main.qml

src.pri

SOURCES += \
    src/main.cpp

In main.cpp got this code

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtDebug>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine(QUrl("qrc:/new/prefix1/qml/main.qml"));
    qDebug() << "Ok engine created";
//    Q_UNUSED(engine)
    return app.exec();
}

main.qml

import QtQuick 2.0
import QtQuick.Controls 1.1

ApplicationWindow{
    title: "sPassKeeper"
    Button{
        text: "Hello"
    }
}

As a result got this output

QML debugging is enabled. Only use this in a safe environment. Ok engine created

Nothing displayed and process is not completed.

标签: qt qml
3条回答
时光不老,我们不散
2楼-- · 2019-06-28 02:12

You're missing this:

ApplicationWindow {
    visible: true

Qt Creator's new project wizard (New File or Project... > Qt Quick Application) has a step where you choose the component set (Select Qt Quick Component Set). If you choose Qt Quick Controls (which are required in order to use ApplicationWindow), Creator generates the correct code for you:

import QtQuick 2.2
import QtQuick.Controls 1.1

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }
}
查看更多
男人必须洒脱
3楼-- · 2019-06-28 02:19

You need to create QQuickWindow from object created by engine. This is a peace of code from my project about it:

  engine.load(QUrl(QString(String_val(_qmlpath))));
  QList<QObject*> xs = engine.rootObjects();
  if (xs.count() == 0) {
    Q_ASSERT_X(false, "Creating C++ runtime", "Your QML file seems buggy");
  }
  QQuickWindow *window = qobject_cast<QQuickWindow*>(xs.at(0) );
  window->showMaximized();
  app.exec();
查看更多
趁早两清
4楼-- · 2019-06-28 02:20

If you want to display root object (Window or ApplicationWindow) in foolscreen mode, then you may do the following:

import QtQuick.Window 2.3
import QtQuick.Controls 2.2

ApplicationWindow {
    visible: true
    visibility: Window.FullScreen
}

Possible useful values for visibility are: Window.FullScreen and Window.Windowed.

查看更多
登录 后发表回答