Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. setting QML properties from c++ before object creation

setting QML properties from c++ before object creation

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    guini
    wrote on 26 Jun 2017, 10:02 last edited by
    #1

    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)

    1 Reply Last reply
    1
    • G Offline
      G Offline
      guini
      wrote on 3 Jul 2017, 15:13 last edited by
      #2

      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";
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved