简体   繁体   English

如何在使用QQmlApplicationEngine时从C ++访问我的Window对象属性?

[英]How can I access my Window object properties from C++ while using QQmlApplicationEngine?

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 idea how 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! 我的问题是我不知道如何访问我的'Window'QML对象,因此,我无法改变它的任何属性或其子项的属性! 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 documentation at all.. QtQuick文档的这一部分显示了从C ++代码中访问QML对象的两种方法,但它们似乎都不适用于这个'QQmlApplicationEngine'加载方案。我也看到人们使用'QApplicationViewer'和'QDeclaritiveView'之类的东西,但我似乎无法在官方文档中找到那些..

I'm getting really frustrated with QtQuick; 我对QtQuick感到非常沮丧; the 'simplicity' of QML seems to be lost in a sea of conflicting documentation and convoluted interface between C++ and QML. QML的“简单性”似乎在C ++和QML之间存在冲突的文档和错综复杂的界面中丢失。 Is there anyway for me to access my QML objects while using the QQmlApplicationEngine method? 无论如何我在使用QQmlApplicationEngine方法时访问我的QML对象? I've tried using 'QuickView', but it doesn't seem to work well with Window QML objects..? 我尝试过使用'QuickView',但它似乎不适用于Window QML对象..? Is QQmlApplicationEngine only useful for QML-only applications in a single file? QQmlApplicationEngine仅对单个文件中的QML应用程序有用吗? So far, every piece of documentation and tutorial I've read has shown something different.. 到目前为止,我读过的每一篇文档和教程都显示了不同的东西......

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'和其他文件中的其他对象)。

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 the QQmlApplicationEngine directly. 通过视图获取您的QML场景的根对象,如果你使用QQuickView或只是QQmlApplicationEngine直接。

  • This next step can be omitted for root objects, but for "qml objects" in general, you will need to have the objectName property set and then you can find any children with the following method: 对于根对象,可以省略下一步,但对于“qml对象”,通常需要设置objectName属性 ,然后可以使用以下方法找到任何子对象:

QList QObject::findChildren(const QString & name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const QList QObject :: findChildren(const QString&name = QString(),Qt :: FindChildOptions options = Qt :: FindChildrenRecursively)const

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

QVariant QObject::property(const char * name) const QVariant QObject :: property(const char * name)const

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: 如果您希望访问的不仅仅是根项目,还需要设置qml对象的objectName属性,如下所示:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    id: mainWindow
    objectName: "mainWindow"
    ...
}

This can be similarly done for any QML object. 对于任何QML对象,都可以这样做。 The key is "objectName" in here. 关键是这里的“objectName”。 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. 您可以省略对于根对象,因为C ++端直接获取根对象,但由于您在问题中引用QML对象,我认为您通常希望解决它。 Once you wish to do the same for any QML object, ie including children, you will need to use the objectName property . 一旦您希望对任何QML对象(包括子对象)执行相同操作,您将需要使用objectName属性

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

相关问题 如何使用 V8 从 C++ 访问和调用 Javascript 对象属性和方法? - How do I access and call Javascript Object properties and methods from C++ using V8? 如何在QQuickItem派生类中访问QQmlApplicationEngine对象? - How can I get access to object of `QQmlApplicationEngine` inside a `QQuickItem` derived class? 问:如何使用 C++ 中的按钮从 window 中打开 window? - Q: How can I open a window from a window using buttons in C++? Qt - 单击QML按钮时如何运行C ++函数? 使用QQmlApplicationEngine - Qt - How to run a C++ function when QML button is clicked? Using QQmlApplicationEngine 除了使用表达式之外,我可以使用C ++访问对象吗? - Can I access an object in C++ other than using an expression? 如何使用C ++将窗口带到Vista的前台? - How can I bring a window to the foreground in Vista using C++? 如何使用 C/C++ 访问 memory 的内容? - How can I access the content of a memory using C/C++? 如何在c ++中从Tracker类创建Tracker对象? - How can I create a Tracker object from my Tracker class in c++? 我可以在Python中创建C ++对象,但无法访问方法 - I can create my C++ object in Python, but can't access methods 使用C ++打开隐藏的COM对象时如何隐藏控制台窗口? - How to hide console window while opening hidden COM object using C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM