如何从C ++访问嵌套的QML对象? [英] How to access a nested QML object from C++?

查看:391
本文介绍了如何从C ++访问嵌套的QML对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是可重现的示例:



main.qml






  import QtQuick 2.0 

Item {
id:root
width:360
height:360

文本{
id:t1
text:qsTr(Hello World)
属性int someNumber:1000
anchors.centerIn: parent
}
MouseArea {
anchors.fill:parent
onClicked:{
Qt.quit();
}
}
}

main.cpp






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

#includeqtquick2applicationviewer.h

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

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

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

qDebug()<< 属性值:< QQmlProperty :: read(object,root.t1.someNumber)。toInt();

return app.exec();
}

我想访问属性 QML Item 文本的somenumber。
上述方法不能产生所需的结果。



如何做?



QML代码扩展

解决方案

根据您的个人偏好,您有两种方法b
$ b

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

  import QtQuick 2.0 

项目{
id:root
width:360
height:360

property别名mySomeNumber:t1.someNumber //这是添加

文本{
id:t1
text:qsTr(Hello World)
属性int someNumber:1000
anchors.centerIn:parent
}
MouseArea {
anchors.fill:parent
onClicked:{
Qt.quit();
}
}
}



C ++代码扩展



由于QML项目 QObject ,您可以明确寻找子项,就像您在C ++ QObject 层次结构。代码如下:

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

#includeqtquick2applicationviewer.h

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

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

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

//这行被添加

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

//分别修改以下行

qDebug()<< 属性值:< QQmlProperty :: read(childObject,someNumber)。toInt();

return app.exec();但是,这意味着您需要添加
}


$ b < objectName:SomeNumberText
行到qml文件中的Text子项。


Here is a reproducible example:

main.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();
        }
    }
}

main.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();
}

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

How to do it?

解决方案

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

QML code extension

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++ 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();
}

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

这篇关于如何从C ++访问嵌套的QML对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆