Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Unable to assign ... to QQmlComponent

    QML and Qt Quick
    2
    3
    6291
    Loading More Posts
    • 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.
    • P
      patrick.gross last edited by

      Hi guys,

      i have the following qml file:

      import QtQuick 2.0
      import QtQuick.Controls 1.4
      
      import "."
      
      TableView {
          property string modelValue
          property var comboBoxModel
          id: tv
      
          TableViewColumn {
              role: "name"
              title: qsTr("Backtrace Data")
              width: (parent.width - 55) / 3
      
          }
          TableViewColumn {
              role: "description"
              title: qsTr("Description")
              width: (parent.width - 55) / 3
          }
          TableViewColumn {
              role: "value"
              title: qsTr("Value")
              width: (parent.width - 55) / 3
      
              delegate: myDelegate
          }
      
          rowDelegate: Rectangle {
              height: Style.table.rowHeight
              SystemPalette {
                  id: myPalette;
                  colorGroup: SystemPalette.Active
              }
              color: {
                  var baseColor = styleData.alternate ? myPalette.alternateBase : myPalette.base
                  return styleData.selected ? myPalette.highlight : baseColor
              }
          }
      
          itemDelegate: Item {
              Text {
                  anchors.verticalCenter: parent.verticalCenter
                  anchors.left: parent.left
                  anchors.leftMargin: 5
                  color: styleData.textColor
                  elide: styleData.elideMode
                  text: styleData.value
                  font.pixelSize: Style.fontSize
              }
          }
      
          Component {
              id:  myDelegate
              Item {
                  id: dummyItem
                  Loader {
                      sourceComponent: {
                          // can happen if the model has not loaded yet
                          if (model === null) {
                              return lineEdit;
                          }
      
                          if (model.componentType === "ComboBox") {
                              var cmb = Qt.createComponent("MyComboBox.qml").createObject(parent);
                              cmb.model = model.comboBoxModel;
                              cmb.anchors.fill = parent;
                              return cmb;
                          }else {
                              var le = Qt.createComponent("LineEdit.qml").createObject(parent);
                              le.anchors.fill = parent;
                              le.text = model.value;
                              //le.width = parent.width;
                              //le.height = parent.height;
                              return le;
                          }
                      }
                  }
              }
          }
      }
      

      I'm trying to dynamically create a Component based on the models componentType, which is currently just a Text or ComboBox. The creation works fine, but I always get warnings like: "Unable to assign ComboBox_QMLTYPE_86 to QQmlComponent"

      Someone had a similar Problem here: https://forum.qt.io/topic/44314/reusing-a-delegate-component-in-qml
      But simply wrapping my loader item in a Component doesn't seem to work for me.
      Any ideas what im doing wrong?

      1 Reply Last reply Reply Quote 0
      • P
        patrick.gross last edited by patrick.gross

        Ok, I got it. Drawing inspiration from this post, this is what I came up with:

        import QtQuick 2.0
        import QtQuick.Controls 1.4
        
        TableView {
            ...
            
            TableViewColumn {
                id: compColumn
                role: "value"
                title: qsTr("Value")
                width: (parent.width - 55) / 3
        
                delegate:   Component {
                    Loader {
                        sourceComponent: {
                            // can happen if the model has not loaded yet
                            if (model === undefined || model === null || parent === null ) {
                                return;
                            }
        
                            return Qt.createQmlObject("import QtQuick 2.0;" +
                                                       "Component {" +
                                                       "Item { Component.onCompleted: loadComponent(this, model)}}", parent);
                        }
                    }
                }
            }
        
          // this function loads the actual component base on model.componentType
            function loadComponent(parent, model) {
                if (model === undefined || model === null) {
                    console.log("model is null or undefined");
                    return;
                }
        
                if (model.componentType === "ComboBox") {
                    var cmb = Qt.createComponent("MyComboBox.qml").createObject(parent);
                    cmb.model = model.comboBoxModel;
                    cmb.anchors.fill = parent;
                } else if (model.componentType === "Text"){
                    var txt = Qt.createQmlObject("import QtQuick 2.0;import QtQuick.Controls 1.4;TextInput{}", parent);
                    txt.text = model.value;
                    txt.anchors.left = parent.left;
                    txt.anchors.leftMargin = 5;
                    txt.anchors.verticalCenter = parent.verticalCenter;
        
                }
            }
        }
        

        Hope this can help out someone else in the future.

        1 Reply Last reply Reply Quote 0
        • D
          DamienL last edited by

          Oooooh yes it helped ! Now I can generate my HMI components with different delegates depending on a specific property. Good for code efficiency and size !

          1 Reply Last reply Reply Quote 0
          • First post
            Last post