首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从C++访问嵌套QML对象?

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

Stack Overflow用户
提问于 2013-12-27 12:36:51
回答 3查看 14.5K关注 0票数 14

下面是一个可重复的例子:

main.qml

代码语言:javascript
复制
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();
        }
    }
}

main.cpp

代码语言:javascript
复制
#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 property Itemtext的命名程序。上述方法没有产生所需的结果。

该怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-27 12:45:33

根据你的个人喜好,你有两种方法(至少)来完成这个任务。

QML码扩展

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

代码语言:javascript
复制
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层次结构中那样。代码应该是这样的:

代码语言:javascript
复制
#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文件中的文本子项中。

票数 11
EN

Stack Overflow用户

发布于 2014-10-24 08:39:29

在这里,您可以找到一个递归方法,通过objectName查找QML项,并从QQmlApplicationEngine::rootObjects()开始。

代码语言:javascript
复制
////
static QQuickItem* FindItemByName(QList<QObject*> nodes, const QString& name)
{
    for(int i = 0; i < nodes.size(); i++){
        // search for node
        if (nodes.at(i) && nodes.at(i)->objectName() == name){
            return dynamic_cast<QQuickItem*>(nodes.at(i));
        }
        // search in children
        else if (nodes.at(i) && nodes.at(i)->children().size() > 0){
            QQuickItem* item = FindItemByName(nodes.at(i)->children(), name);
            if (item)
                return item;
        }
    }
    // not found
    return NULL;
}

///
static QQuickItem* FindItemByName(QQmlApplicationEngine* engine, const QString& name)
{
    return FindItemByName(engine->rootObjects(), name);
}
票数 2
EN

Stack Overflow用户

发布于 2014-11-30 15:40:15

这个用例是什么?最好将文本、someNumber结构或对象作为模型来处理。然后,您只需要找到模型对象。或者您可以在C++端创建模型对象,并在QML上下文中设置它。您可以访问QML中的模型及其嵌套属性:

代码语言:javascript
复制
Text {
        text: model.text
        property int someNumber: model.someNumber
    }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20800850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档