1

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?

0

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

2 Comments

It looks like engine does not have a rootObjects() member, only rootContext()
Yes, rootObjects() appeared in QQmlAllicationEngine. I fixed samples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.