使用 QQmlApplicationEngine 时如何从 C++ 访问我的 Window 对象属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23177839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access my Window object properties from C++ while using QQmlApplicationEngine?
提问by MrKatSwordfish
I've been trying to learn QtQuick for GUI creation, but I've been having a hard time understanding how to interact with QML objects from the C++ part of my test program.
我一直在尝试学习 QtQuick 来创建 GUI,但是我一直很难理解如何从我的测试程序的 C++ 部分与 QML 对象进行交互。
Here's my simple QML file:
这是我的简单 QML 文件:
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
id: mainWindow
visible: true
width: 800
height: 800
color: "#FFFF0000"
MouseArea {
anchors.fill: parent
onClicked: Qt.quit()
}
Rectangle {
id: testRect
width: 100
height: 100
anchors.centerIn: parent
color: "#FF0000FF"
}
}
Here's the basic C++ file that came with it (auto-generated by QtCreator):
这是随附的基本 C++ 文件(由 QtCreator 自动生成):
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
My issue is that I have no ideahow to gain access to my 'Window' QML object, and as a result, I'm unable to alter any of its properties or the properties of its children! This part of the QtQuick documentation shows two methods of accessing QML objects from within C++ code, but neither of them seem to apply to this 'QQmlApplicationEngine' loading scheme.. I've also seen people use things like 'QApplicationViewer' and 'QDeclaritiveView', but I can't seem to find those in the official documentationat all..
我的问题是我不知道如何访问我的“窗口”QML 对象,因此,我无法更改其任何属性或其子项的属性!QtQuick 文档的这一部分显示了从 C++ 代码中访问 QML 对象的两种方法,但它们似乎都不适用于此“QQmlApplicationEngine”加载方案。我还看到人们使用诸如“QApplicationViewer”和“QDeclaritiveView”之类的东西,但我似乎根本找不到官方文档中的那些..
I'm getting really frustrated with QtQuick; the 'simplicity' of QML seems to be lost in a sea of conflicting documentation and convoluted interface between C++ and QML. Is there anyway for me to access my QML objects while using the QQmlApplicationEngine method? I've tried using 'QuickView', but it doesn't seem to work well with Window QML objects..? Is QQmlApplicationEngine only useful for QML-only applications in a single file? So far, every piece of documentation and tutorial I've read has shown something different..
我对 QtQuick 感到非常沮丧;QML 的“简单性”似乎在 C++ 和 QML 之间相互冲突的文档和错综复杂的接口的海洋中消失了。无论如何,我可以在使用 QQmlApplicationEngine 方法时访问我的 QML 对象吗?我试过使用“QuickView”,但它似乎不适用于 Window QML 对象......?QQmlApplicationEngine 仅对单个文件中的 QML 应用程序有用吗?到目前为止,我读过的每一份文档和教程都展示了一些不同的东西。
Any help or clarification would be appreciated. Ideally I'd like to know how to access and modify QML objects (like 'mainWindow', 'testRect', and other objects in other files) via my C++ code.
任何帮助或澄清将不胜感激。理想情况下,我想知道如何通过我的 C++ 代码访问和修改 QML 对象(如“mainWindow”、“testRect”和其他文件中的其他对象)。
回答by lpapp
Turning my comment into a proper answer: this is usually done by two methods:
将我的评论变成正确的答案:这通常通过两种方法完成:
Get the root object of your QML scene through a view if you use
QQuickView
or just theQQmlApplicationEngine
directly.This next step can be omitted for root objects, but for "qml objects" in general, you will need to have the objectName propertyset and then you can find any children with the following method:
如果您使用
QQuickView
或QQmlApplicationEngine
直接使用视图,则通过视图获取 QML 场景的根对象。对于根对象,可以省略下一步,但对于一般的“qml 对象”,您需要设置objectName 属性,然后您可以使用以下方法找到任何子项:
C++ side
C++端
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
// Step 1: get access to the root object
QObject *rootObject = engine.rootObjects().first();
QObject *qmlObject = rootObject->findChild<QObject*>("mainWindow");
// Step 2a: set or get the desired property value for the root object
rootObject->setProperty("visible", true);
qDebug() << rootObject->property("visible");
// Step 2b: set or get the desired property value for any qml object
qmlObject->setProperty("visible", true);
qDebug() << qmlObject->property("visible");
return app.exec();
}
See the documentation for property set and get in the official documentation:
请参阅属性集的文档并在官方文档中获取:
bool QObject::setProperty(const char * name, const QVariant & value)
bool QObject::setProperty(const char * name, const QVariant & value)
and
和
Good, we are now more or less done on the C++ side.
好的,我们现在或多或少地完成了 C++ 方面的工作。
QML Side
QML端
You will also need to have the objectName
property of your qml objects set if you wish to access more than just the root item as follows:
objectName
如果您希望访问的不仅仅是根项目,您还需要设置 qml 对象的属性,如下所示:
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
id: mainWindow
objectName: "mainWindow"
...
}
This can be similarly done for any QML object. The key is "objectName" in here. You could omit that for the root object as the C++ side gets the root object directly, but since you are referring to QML objects in your question, I assume that you would like to solve it in general. Once you wish to do the same for any QML object, i.e. including children, you will need to use the objectName property.
对于任何 QML 对象,这可以类似地完成。这里的关键是“objectName”。对于根对象,您可以省略它,因为 C++ 端直接获取根对象,但是由于您在问题中指的是 QML 对象,因此我假设您一般想解决它。一旦您希望对任何 QML 对象(即包括子对象)执行相同操作,您将需要使用objectName 属性。