details
details

Reputation: 23

Set property of qml object using c++

I have tried with

 widget->setProperty("text1Text", QVariant("After..."));

in my C++, and

 Button
 {
    property alias text1Text: text1.text
    Text
    {
        id: text1
        text: "Initial"
    }   
 }

in qml. widget is a QQuickWidget object. What am I doing wrong?

Upvotes: 1

Views: 3650

Answers (1)

Dmitry Sokolov
Dmitry Sokolov

Reputation: 3190

See Interacting with QML Objects from C++.

If you are using QQmlEngine:

// Using QQmlComponent
QQmlApplicationEngine engine;
...
QObject * root = engine.rootObjects().first();

If you are using QQuickView:

QQuickView view;
...
QObject * root = view.rootObject();

Getting text1:

// Update Qml file
Text
{
    id: text1
    text: "Initial"
    objectName: id
} 

// Find text1 in c++
QObject * o1 = root->findChild<QObject *>(QStringLiteral("text1"));
QQuickItem *text1 = qobject_cast<QQuickItem*>(o1);

// Set property
text1->setProperty("text", QVariant());

Upvotes: 1

Related Questions