【发布时间】:2014-01-15 01:19:03
【问题描述】:
这是一个可重现的例子:
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 Item 的 text 的某个编号的 property。
上述方法没有产生预期的结果。
怎么做?
【问题讨论】:
-
为什么不是属性别名?
-
@LaszloPapp 不明白,请解释一下。
-
属性别名 mytext: t1.text 用于根项,或 'QObject *object = object->findChild
("t1");` -
在 QML 中,您将
Text命名为“t1”,但在 C++ 中,您将其访问为“ti”。错字? -
@StefanMonov 这可能是错字。感谢您指出。
标签: c++ qt qml qtquick2 qtcore