【问题标题】:How to change title of a QtQuick 2 window?如何更改 QtQuick 2 窗口的标题?
【发布时间】:2013-10-03 13:02:26
【问题描述】:

我正在尝试更改我的项目的默认窗口,但它不起作用。 我正在使用 QtQuick 2.0。 尝试导入 QtQuick.Window 2.0 并将 Window{} 作为根对象而不是 Rectangle{} 但它不允许将窗口对象作为根。它给了我以下错误:

QQuickView only supports loading of root objects that derive from QQuickItem. 

If your example is using QML 2, (such as qmlscene) and the .qml file you 
loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur. 

To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the 
QDeclarativeView class in the Qt Quick 1 module.

关于如何更改窗口标题的任何想法?我正在使用 Qt 5.1.1。

【问题讨论】:

    标签: c++ qt qml qtquick2 qquickview


    【解决方案1】:

    这取决于您希望如何使用您的 GUI。如果您想将 QML 用于几乎所有事情,从窗口创建到窗口中的元素,以下解决方案可能是您的最佳选择。

    Qt5.1,仅适用于桌面

    如果您有 Qt5.1,您可以使用来自 QtQuick.Controls 的新 ApplicationWindow 项目作为您在名为 ma​​in.qml 的文件中的根对象:

    import QtQuick 2.0
    import QtQuick.Controls 1.0
    
    ApplicationWindow {
        visible: true
        width: 360
        height: 360
        title: "MyWindow"
    
        Text {
            text: "Hello world!"
            anchors.centerIn: parent
        }
    }
    

    为避免您收到错误消息,您需要使用QQmlApplicationEngine 而不是QQuickView 来启动您的应用程序。这可以在您的 ma​​in.cpp 文件中按如下方式完成:

    #include <QtGui/QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {    
        QGuiApplication app(argc, argv);    
        QQmlApplicationEngine engine("main.qml");    
        return app.exec();
    }
    

    Qt5.0,(可能)用于桌面以外的环境

    如果您不适合使用 Qt5.1,或者您的目标设备尚不支持 QtQuick.Controls,则替代方法是按以下方式使用 Window。将此添加到 ma​​in.qml:

    import QtQuick 2.0
    import QtQuick.Window 2.0
    
    Window {
        visible: true
        width: 360
        height: 360
        title: "MyWindow"
    
        Text {
            text: "Hello world!"
            anchors.centerIn: parent
        }
    }
    

    让这成为你的 ma​​in.cpp:

    #include <QtGui/QGuiApplication>
    #include <QQmlEngine>
    #include <QQmlComponent>
    
    int main(int argc, char *argv[])
    {    
        QGuiApplication app(argc, argv);    
        QQmlEngine engine;
        QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
        component.create();    
        return app.exec();
    }
    

    这应该会打开一个带有文本“Hello World!”的窗口。

    【讨论】:

      【解决方案2】:

      Qt 5.1.1 附带的“Qt Quick Controls - Gallery”示例提供了一个很好的示例。以下代码假定项目结构基于“Qt Quick 2 APplication (Built-in Types)”模板。

      在 ma​​in.qml 中,使用:

      ApplicationWindow {
          title: "Component Gallery"
      ...
      

      在 ma​​in.cpp 中,使用:

      #include <QtQml>
      #include <QtQuick/QQuickView>
      #include <QtCore/QString>
      #include <QtWidgets/QApplication>
      #include "qtquick2applicationviewer.h"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          QQmlApplicationEngine engine("qml/YourProject/main.qml");
          QObject* topLevel = engine.rootObjects().value(0);
          QQuickWindow* window = qobject_cast<QQuickWindow*>(topLevel);
          window->show();
          return app.exec();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多