如何在 QWidget 中插入 QML 视图 [英] How to insert QML view in a QWidget
问题描述
我是 QML 的初学者并尝试在 QWdiget 中插入 QML 视图,但我不明白为什么它不起作用.
I am a beginner in QML and try to insert a QML View in QWdiget but I don't understand why it doesn't work.
这是我的 qml 文件的一个简单示例(这不是真正的文件):
Here is a simple example of my qml file (this is not the real file):
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
}
}
这就是我在主窗口中加载它的方式:
And this is how I load it in my mainwindow:
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 项目.
How could I insert my view in a QWidget ? At this time, I really need to use a QML view and because I need to use it in an already existing application, I can't juste use a QML project.
非常感谢您的帮助,祝您有美好的一天!
Thanks a lot for your help and have a good day !
推荐答案
存在一个特殊的QQuickWidget
,专门用于这个目的.
There exist a special QQuickWidget
, dedicated to that exact purpose.
QQuickWidget *view = new QQuickWidget;
view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
view->show();
这篇关于如何在 QWidget 中插入 QML 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!