如何运行导入 QtQuick2.0 和 QtQuick.Controls 1.1 的应用程序

Posted

技术标签:

【中文标题】如何运行导入 QtQuick2.0 和 QtQuick.Controls 1.1 的应用程序【英文标题】:How to run an application which import QtQuick2.0 and QtQuick.Controls 1.1 【发布时间】:2014-01-23 15:07:45 【问题描述】:

我是第一次创建 qtquick 应用程序。我使用 QtCreator 3.0、Qt5.2.0 和 MSVC2012。 当我编写以下代码时,我收到错误消息。我明白它在说什么。但如果可能的话,我想使用 QtQuick 2.0 而不是 1.0。

有谁知道如何解决这个错误? 任何帮助,将不胜感激。提前致谢。

示例代码

[main.qml]

import QtQuick 2.0
import QtQuick.Controls 1.1

ApplicationWindow 
    width: 360
    height: 360
    menuBar:MenuBar 
        Menu 
            title: "File"
            MenuItem  text: "Open..." 
            MenuItem  text: "Close" 
        

        Menu 
            title: "Edit"
            MenuItem  text: "Cut" 
            MenuItem  text: "Copy" 
            MenuItem  text: "Paste" 
        
    
    Text 
        text: qsTr("Hello World")
        anchors.centerIn: parent
    
    MouseArea 
        anchors.fill: parent
        onClicked: 
            Qt.quit();
        
    

[main.cpp]

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"

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

    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/QtQuickAppTest/main.qml"));
    viewer.showExpanded();

    return app.exec();

错误信息

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. 

【问题讨论】:

【参考方案1】:

您需要使用选项“Qt Quick Control 1.0”而不是“Qt Quick 2.0”来生成 Qt Quick Application 项目。它将生成QtQuick2ControlsApplicationViewer 类,它不使用QQuickView,而只是使用QQmlComponent

【讨论】:

感谢您的建议。当我使用选项“Qt Quick Control 1.0”创建项目时它可以工作。我误解了组件集。

以上是关于如何运行导入 QtQuick2.0 和 QtQuick.Controls 1.1 的应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 QtQuick 2 窗口的标题?

使用 SVG 插件部署 QtQuick 2.0 应用程序

QtQuick 2.0 下拉按钮

使用 PyQt 将基于 qml 的图表集成到现有的 ui

QtConcurrent 错误:对非静态成员的引用

如何使用 android studio 导入和运行项目