3

I have QML file which describes button(moduleButton.qml):

import QtQuick 2.0
Rectangle {
    id: button;
    width: 100; height: 20
    Text {
        id: buttonText;
        text: "Hello World";
    }
}

From other QML form I load this button via Qt.createComponent method:

var moduleButton = Qt.createComponent("moduleButton.qml");
moduleButton.createObject(mainRect);

I tried to set/get width of moduleButton:

moduleButton.width = 30;

But received following error: Cannot assign to non-existent property "width"

How to access dynamic object attributes and child elements?

P.S. Qt.createQmlObject method perfectly works, but I need to load QML from file, not from string.

1
  • createQmlObject Commented May 27, 2019 at 5:20

1 Answer 1

5

createObject() returns the new object. Your code should look like this:

var moduleButton = Qt.createComponent("moduleButton.qml");
var myButton = moduleButton.createObject(mainRect);

myButton.width = 40

The moduleButton is a component (a factory), used to instantiate the item.

Documentation: Dynamic QML Object Creation from JavaScript

Sign up to request clarification or add additional context in comments.

Comments

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.