Important: Please read the Qt Code of Conduct -

QML Loader: binding is set only during loading, but is not linked

  • Hi,

    I have the following loader:

    Loader {
    source: "myWindow.qml"
    onLoaded: { item.title= myCppObject.title }
    When myWindow is loaded the title is set, but when I change the title using myCppObject the title is not changed!

    class MyCppObject : public QObject
    Q_PROPERTY(QString title READ Title NOTIFY TitleChanged)

    MyCppObject (QObject* aParent = 0);
    virtual ~MyCppObject ();

             void SetTitle(const QString & aTitle);

    public slots:
    const QString & Title();


             void TitleChanged();

    QString mTitle;

    void MyCppObject::SetTitle(const QString & aTitle)
    mTitle = aTitle;
    emit TitleChanged();

    const QString & MyCppObject::Title()
    return Title;

    Before I haven't used the loader but just created all my windows at once during startup of my application. When I emitted a signal from c++ the QML object was notified and updated. When I use the loader and the onLoaded/item. construction the QML object isn't notified anymore when a change occurs.

  • Found the answer. Use Binding:

    Binding {
    target: loader.item
    property: "model"
    value: model
    when: loader.status == Loader.Ready

  • Yes, the assignment form in your first post won't cause a binding. In QtQuick 1.x you can use a Binding element, and in QtQuick 2.0 you can either use a Binding element, or use the "initial property values" argument to the setSource function of the Loader, along with the Qt.binding() function, to generate a binding.

Log in to reply