Change QML object 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. 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 s hould. 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.1Window {
visible: true
width: 360
height: 360MouseArea { anchors.fill: parent onClicked: { Qt.quit(); } } Text { id: whot text: "" anchors.centerIn: parent font.pixelSize: 20 color: "green" }
}@