[英]How to set QML properties from C++
I'm trying to do a simple task as changing a property (text: ) of some QML object from C++ yet I'm failing miserably.我正在尝试做一个简单的任务,比如从 C++ 更改某些 QML 对象的属性(文本:),但我失败了。 Any help appreciated.
任何帮助表示赞赏。
I'm not getting any errors, the window shows up, just the text property doesn't change as (at least I think) it should.我没有收到任何错误,窗口显示出来,只是 text 属性没有像(至少我认为)应该的那样改变。 Is even anything I'm NOT doing wrong here?!!
有什么我没有做错的吗?!
What I was trying is this:我正在尝试的是这样的:
main.cpp主程序
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickView>
#include <QQuickItem>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QString>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlComponent component(&engine, QUrl::fromLocalFile("main.qml"));
QObject *object = component.create();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QString thisString = "Dr. Perry Cox";
object->setProperty("text", thisString); //<--- tried instead of thisString putting "Dr. ..." but nope.
delete object;
return app.exec();
}
main.qml主文件
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
visible: true
width: 360
height: 360
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
Text {
id: whot
text: ""
anchors.centerIn: parent
font.pixelSize: 20
color: "green"
}
}
When you call QObject *object = component.create();
当你调用
QObject *object = component.create();
you get access to the root context, which is the Window
component and its properties.您可以访问根上下文,即
Window
组件及其属性。
To get access to Text
properties, you can create property alias like this:要访问
Text
属性,您可以像这样创建属性别名:
Window {
property alias text: whot.text
...
Text {
id: whot
text: ""
...
}
}
That will give you access to whot
's text
property from within the Window
's context.这将使您能够从
Window
的上下文中访问whot
的text
属性。
There is another slightly more round-about way.还有另一种稍微迂回的方式。 Assign
objectName
property instead of id
(or both if you still need id
) to whot
:将
objectName
属性而不是id
(或者如果您仍然需要id
)分配给whot
:
Text {
id: whot // <--- optional
objectName: "whot" // <--- required
text: ""
...
}
Now you can do this in code:现在您可以在代码中执行此操作:
QObject *whot = object->findChild<QObject*>("whot");
if (whot)
whot->setProperty("text", thisString);
On a side note: I don't think you are supposed to delete the object
until after calling app.exec()
.附带说明:我认为您不应该在调用
app.exec()
之前删除该object
。 Otherwise, it will ... well, be deleted.否则,它将......好吧,被删除。 :)
:)
#include <QQmlContext>
#include <qquickview.h>
#include <qdir.h>
QQmlApplicationEngine engine;
QString root = QCoreApplication::applicationDirPath();
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
QObject *topLevel = engine.rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow*>(topLevel);
window->setProperty("root", root);
for qml对于 qml
ApplicationWindow {
property string root
onRootChanged: {
console.log("root : "+ root);
}
}
对于 QML 属性,您应该改用QQmlProperty :
QQmlProperty::write(whot, "text", thisString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.