下面是一个可重复的例子:
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();
}我希望访问QML property Item的text的命名程序。上述方法没有产生所需的结果。
该怎么做呢?
发布于 2013-12-27 12:45:33
根据你的个人喜好,你有两种方法(至少)来完成这个任务。
QML码扩展
可以将属性别名添加到根项,如下所示:
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层次结构中那样。代码应该是这样的:
#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文件中的文本子项中。
发布于 2014-10-24 08:39:29
在这里,您可以找到一个递归方法,通过objectName查找QML项,并从QQmlApplicationEngine::rootObjects()开始。
////
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);
}发布于 2014-11-30 15:40:15
这个用例是什么?最好将文本、someNumber结构或对象作为模型来处理。然后,您只需要找到模型对象。或者您可以在C++端创建模型对象,并在QML上下文中设置它。您可以访问QML中的模型及其嵌套属性:
Text {
text: model.text
property int someNumber: model.someNumber
}
https://stackoverflow.com/questions/20800850
复制相似问题