简体   繁体   English

Qt5将QWidget与QtQuick2一起使用

[英]Qt5 Using QWidget with QtQuick2

There are a few problems with the combination of Qt5 and QtQuick2. Qt5和QtQuick2的组合存在一些问题。 Whenever you would like to use some of the many QWidget based classes you first run across the following problem: 每当您想要使用许多基于QWidget的类中的一些时,您首先遇到以下问题:

You should be using QApplication instead of Qt5's QGuiApplication . 您应该使用QApplication而不是Qt5的QGuiApplication Well that can be easly changed, right? 嗯,这很容易改变,对吧?

So now I am using QApplication but whenever I try to use a QWidget based class my program either crashes or results in some nonsense error messages. 所以现在我正在使用QApplication但每当我尝试使用基于QWidget的类时,我的程序会崩溃或导致一些无意义的错误消息。

How should I use the old QWidgets with Qt5 then? 那么我应该如何在Qt5中使用旧的QWidgets呢? I know that they are not the best solution with Qt5 but they are quite useful... 我知道它们不是Qt5的最佳解决方案,但它们非常有用......

PS I am developing my app in Linux, for all platforms. PS我正在Linux中为所有平台开发我的应用程序。
I am also using the auto-generated QtQuick2ApplicationViewer class to render out QtQuick 2.0 based applications. 我还使用自动生成的QtQuick2ApplicationViewer类来渲染基于QtQuick 2.0的应用程序。

In Qt 5.1 (and presumably from now on) you should use QWidget::createWindowContainer. 在Qt 5.1中(大概从现在开始)你应该使用QWidget :: createWindowContainer。 Your application should be a QWidget based application and put the QML inside QWidgets. 您的应用程序应该是基于QWidget的应用程序,并将QML放在QWidgets中。 Putting QWidgets into a QML application is not supported.See this blog entry . 不支持将QWidgets放入QML应用程序。请参阅此博客条目

If you have a form class and you want to put qml into the container widget. 如果您有一个表单类,并且想要将qml放入容器窗口小部件中。

If you have this QML: 如果你有这个QML:

import QtQuick 2.0

Rectangle {
    property alias text: textItem.text
    width: 156
    height: 35
    Text {
        width: 150
        height: 20
        text: qsTr("Hello World")
        id: textItem
    }
}

In a file called myqml.qml, then put the path to it in to the qml prefix of a resource file. 在名为myqml.qml的文件中,然后将其路径放入资源文件的qml前缀中。

Then put in the form constructor: 然后放入表单构造函数:

ui->setupUi(this); // as normal
QQuickView* view = new QQuickView();
QWidget* widget = QWidget::createWindowContainer(view, ui->container);
view->setSource(QUrl("qrc:/qml/myqml.qml"));
if(view->status()!=QQuickView::Ready)
    qDebug("can't initialise view");
widget->setMinimumSize(500,100);
QQuickItem* container = view->rootObject();

Then when you want to interact with the QML: 然后,当您想要与QML交互时:

container->setProperty("text", "Hello alternate universe");

It seems this is a known bug: https://bugreports.qt.io/browse/QTBUG-25643 这似乎是一个已知的错误: https//bugreports.qt.io/browse/QTBUG-25​​643

That also seems to have links to possible solutions/workarounds, but I did not check them. 这似乎也与可能的解决方案/解决方法有关,但我没有检查它们。

About QWidgets and "they are not the best solution": First of all, until future Qt 5.1 (or which ever version it will be) with complete desktop Qt Quick components, they're the only sensible option for regular desktop apps, and only option is by definition best option ;) 关于QWidgets和“它们不是最好的解决方案”:首先,在未来的Qt 5.1(或者它将会是哪个版本)中使用完整的桌面Qt Quick组件,它们是常规桌面应用的唯一合理选择,并且只有选项是定义最佳选项;)

But even in future QWidgets are 100% valid choice for a Qt5 app. 但即使在未来,QWidgets也是Qt5应用的100%有效选择。 Only thing with them is, they're unlikely to develop much, so what you have now, is the best you're going to ever have with them. 与他们唯一相关的是,他们不太可能发展得太多,所以你现在所拥有的,是他们将要拥有的最好的。 If that's fine, then they're the solution to choose. 如果那很好,那么他们就是选择的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM