0
我是QML的初学者,并尝试在QWdiget中插入QML视图但我不明白为什么它不起作用。如何在QWidget中插入QML视图
这里是我的QML文件的一个简单的例子(这不是真正的文件):
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.2
import QtQml.Models 2.1
ObjectModel {
id: itemModel
Rectangle {
color: "orange"
anchors.fill: parent
}
Rectangle {
color: "orange"
anchors.fill: parent
}
Rectangle {
color: "orange"
anchors.fill: parent
}
}
ListView {
id: my_list
anchors.fill: parent
model: itemModel
}
}
这就是我如何加载它在我的主窗口:
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200, 200);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("main.qml"));
ui->dockWidget->setWidget(container);
如何我可以在QWidget中插入我的视图吗? 目前,我确实需要使用QML视图,因为我需要在已经存在的应用程序中使用它,所以我不能使用QML项目。
非常感谢您的帮助和美好的一天!
您好! 感谢您的回答。但是,即使我尝试使用QQuickWidget,我也遇到以下错误:'Syntax error:ListView {' 我不明白这里有什么问题。 –
您的代码没有根项目。另外,由于QQuickWidget是一个QQuickWindow的包装,我假定根项目必须是一个窗口,或者是一个ApplicationWindow,或者一个简单的Window。 – dtech
这很奇怪,因为我已经试过了。 但是,如果我使用作为根项目的窗口,我有以下错误信息:'QQuickWidget只支持加载从QQuickItem派生的根对象。 如果您的示例使用QML 2(例如qmlscene)和您加载的 具有'import QtQuick 1.0'或'import Qt 4.7'的.qml文件,则会发生此错误。 要使用'import QtQuick 1.0'或'import Qt 4.7'加载文件,请在Qt Quick 1模块中使用 QDeclarativeView类。 ' 但我没有使用QtQuick 1.0或Qt 4.7。 如果我使用DeclarativeView,它将不支持'import QtQuick 2.4' –