如何从 C++ 访问嵌套的 QML 对象?

2022-01-19 00:00:00 qt qtquick2 qml c++ qtcore

这是一个可重现的例子:

Here is a reproducible example:

ma??in.qml

import QtQuick 2.0

Item {
    id : root
    width: 360
    height: 360

    Text {
        id : t1
        text: qsTr("Hello World")
        property int someNumber: 1000
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}

ma??in.cpp

#include <QtGui/QGuiApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QDebug>

#include "qtquick2applicationviewer.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml"));
    viewer.showExpanded();

    QQmlEngine engine;
    QQmlComponent component(&engine, "qml/untitled/main.qml");
    QObject *object = component.create();

    qDebug() << "Property value:" << QQmlProperty::read(object, "root.t1.someNumber").toInt();

    return app.exec();
}

我希望访问 QML Itemtext 的一些 property.上述方法没有产生预期的结果.

I wish to access the property somenumber of the text of the QML Item. The above method isn't producing the desired result.

怎么做?

推荐答案

根据您的个人喜好,您有(至少)两种方法来完成此任务.

You have two ways (at least) to accomplish this depending on your personal preference.

您可以为根项添加属性别名,如下所示:

You can add a property alias to the root item as follows:

import QtQuick 2.0

Item {
    id : root
    width: 360
    height: 360

    property alias mySomeNumber: t1.someNumber // This is the addition

    Text {
        id : t1
        text: qsTr("Hello World")
        property int someNumber: 1000
        anchors.centerIn: parent
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}

C++ 代码扩展

由于 QML 项是 QObject,因此您也可以显式查找子项,就像在 C++ QObject 层次结构中那样.代码是这样的:

C++ code extension

Since the QML items are QObject, you can look for the children explicitly as well, just as you would do it in a C++ QObject hierarchy. The code would be something like this:

#include <QtGui/QGuiApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlProperty>
#include <QDebug>

#include "qtquick2applicationviewer.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/untitled/main.qml"));
    viewer.showExpanded();

    QQmlEngine engine;
    QQmlComponent component(&engine, "qml/untitled/main.qml");
    QObject *object = component.create();

    // This line is added

    QObject *childObject = object->findChild<QObject*>("SomeNumberText");

    // The following line is modified respectively

    qDebug() << "Property value:" << QQmlProperty::read(childObject, "someNumber").toInt();

    return app.exec();
}

但是,这意味着您需要将 objectName: "SomeNumberText" 行添加到 qml 文件中的 Text 子项.

However, this means you will need to add the objectName: "SomeNumberText" line to your Text child item in the qml file.

相关文章