Qt Quick 示例 - 嵌入到小工具中
演示通过QWidget::createWindowContainer() 将QQuickWindow 嵌入QWidget UI。
本示例演示了在基于QWidget 的应用程序中添加Qt Quick 内容的方法之一。QQuickView QQuickWindow 和它的父类QWindow 派生而来。这意味着它们可以与QWidget::createWindowContainer() 一起使用,就像其他QWindow 一样。
运行示例
运行示例 Qt Creator,打开Welcome 模式并从Examples 中选择示例。更多信息,请参阅Qt Creator: 教程:构建并运行。
在内部嵌入窗口会导致在窗口部件层次结构中创建一个本地子窗口部件,并且窗口(示例中为QQuickView )会被重新转属。容器窗口部件负责调整子窗口的位置,使其看起来像是用户界面的一部分,尽管实际上它是一个独立的本地窗口,可以独立于其父窗口(即基于QWidget 的顶层窗口)进行绘制。
MainWindow::MainWindow() : m_quickView(new QQuickView) { QWidget *centralWidget = new QWidget(this); QVBoxLayout *layout = new QVBoxLayout(centralWidget); m_quickView->setResizeMode(QQuickView::SizeRootObjectToView); connect(m_quickView, &QQuickView::statusChanged, this, &MainWindow::quickViewStatusChanged); connect(m_quickView, &QQuickWindow::sceneGraphError, this, &MainWindow::sceneGraphError); m_quickView->loadFromModule("embeddedinwidgets", "Main"); QWidget *container = QWidget::createWindowContainer(m_quickView); container->setMinimumSize(m_quickView->size()); container->setFocusPolicy(Qt::TabFocus); layout->addWidget(new QLineEdit(QStringLiteral("A QLineEdit"))); layout->addWidget(container); layout->addWidget(new QLineEdit(QStringLiteral("A QLineEdit"))); setCentralWidget(centralWidget); QMenu *fileMenu = menuBar()->addMenu(tr("File")); fileMenu->addAction(tr("Quit"), qApp, &QCoreApplication::quit); }
初始化过程中的关键步骤是创建container
,这是一个包裹并承载QQuickView 的QWidget 。然后,该窗口小部件就可以像其他QWidget 一样添加到布局中。
注: 使用本地窗口嵌入只是结合QWidget 和Qt Quick 用户界面的一种可能方法。另一种更常用的方法是QQuickWidget 。有关使用QQuickWidget 的示例,请参阅Qt Quick Widgets 示例。QQuickWidget 完全不使用本地窗口,而是将Qt Quick 渲染重定向到纹理中,然后通过 OpenGL 或 Vulkan 等 3D 图形应用程序接口与QWidget 的其他内容合成。这带来了更大的灵活性,但牺牲了性能。它还适用于没有实际窗口系统和本地窗口概念的平台。本示例中演示的窗口嵌入方法性能更高,但它最适用于Qt Quick 内容占据固定矩形区域且不会调整大小、堆叠或剪切的用户界面。
另请参见 QWidget::createWindowContainer().
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.