setting QML properties from c++ before object creation



  • I'm loading a QML file from C++ using QQmlComponent::beginCreate(). I want to set some properties before calling componentComplete(). This does not work, when the property I want to set has a property binding in the qml file.

    A small example:

    I have the following QML Class:

    import QtQuick 2.5
    
    Item {
        id: root
    
        x: 50
        y: 100
    
        height: 300
        width: 400
    
        property color c1: "red"
        property color c2: c1
    
        Rectangle {
            id: rect1
            color: root.c1
    
            x: 0
            y: 0
            width: 200
            height: root.height
        }
    
        Rectangle {
            id: rect2
            color: root.c2
    
            x: 200
            y: 0
            width: 200
            height: root.height
        }
    }
    

    and load it like this

    void Loader::loadElement() {
        QQmlComponent component(engine_, "qrc:/Element.qml");
        QQuickItem* topRect = engine_->rootObjects()[0]->findChild<QQuickItem*>("topRect");
    
        QQuickItem* element = qobject_cast<QQuickItem*>(component.beginCreate(engine_->rootContext()));
    
        element->setParent(topRect);
        element->setParentItem(topRect);
        element->setProperty("c1", "green");
        element->setProperty("c2", "blue");
    
        component.completeCreate();
        QQmlEngine::setObjectOwnership(element, QQmlEngine::JavaScriptOwnership);
    
        qDebug() << "done";
    }
    

    Both c1 and c2 are set to green after loading. When I set c2 to a static value (e.g property color c2: "black")in the QML File, everything works as expected. When I call setProperty() after completeCreate() it also works.
    (In my code I need to set the properties before object creation, because I do some checks of the properties on object creation.)

    This behaviour seems wrong to me. Is this a bug?
    If it's not a bug, is there some other way to set QML properties from C++ before object creation?

    (I don't have the privileges to upload my example, sorry)



  • Hey,

    I found a solution. When I use a QQmlProperty to set the properties everything works as expected :-)

    void Loader::loadElement() {
        QQmlComponent component(engine_, "qrc:/Element.qml");
        QQuickItem* topRect = engine_->rootObjects()[0]->findChild<QQuickItem*>("topRect");
    
        QQuickItem* element = qobject_cast<QQuickItem*>(component.beginCreate(engine_->rootContext()));
    
        QQmlProperty prop1(element, "c1", engine_);
        QQmlProperty prop2(element, "c2", engine_);
    
        element->setParent(topRect);
        element->setParentItem(topRect);
    
        // element->setProperty("c1", "green");
        // element->setProperty("c2", "blue");
        
        prop1.write("green");
        prop2.write("blue");
    
    
        component.completeCreate();
        QQmlEngine::setObjectOwnership(element, QQmlEngine::JavaScriptOwnership);
    
        qDebug() << "done";
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.