Qt QQmlApplicationEngine拒绝显示qml代码

时间:2014-07-05 09:28:48

标签: qt qml

我有这个专业档案

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

在src pri和qml pri中只有单行 qml.pri

OTHER_FILES += \
    qml/main.qml

src.pri

SOURCES += \
    src/main.cpp

在main.cpp中得到了这段代码

#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"
    }
}

结果得到了这个输出

  

启用QML调试。只能在安全的环境中使用它。好   引擎创建

未显示任何内容且未完成处理。

3 个答案:

答案 0 :(得分:7)

你错过了这个:

ApplicationWindow {
    visible: true

Qt Creator的新项目向导(New File or Project... > Qt Quick Application)有一个步骤,您可以选择组件集Select Qt Quick Component Set)。如果您选择Qt Quick Controls(为了使用ApplicationWindow而需要),Creator会为您生成正确的代码:

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
    }
}

答案 1 :(得分:2)

您需要从引擎创建的对象创建QQuickWindow。这是我项目中的peace of code

  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();

答案 2 :(得分:0)

如果要在foolscreen模式下显示根对象(WindowApplicationWindow),则可以执行以下操作:

import QtQuick.Window 2.3
import QtQuick.Controls 2.2

ApplicationWindow {
    visible: true
    visibility: Window.FullScreen
}

visibility的可能有用值包括:Window.FullScreenWindow.Windowed