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. Unable to assign ... to QQmlComponent
Forum Updated to NodeBB v4.3 + New Features

Unable to assign ... to QQmlComponent

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 7.5k Views 1 Watching
  • 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 Offline
    P Offline
    patrick.gross
    wrote on last edited by
    #1

    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
    0
    • P Offline
      P Offline
      patrick.gross
      wrote on last edited by patrick.gross
      #2

      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
      0
      • D Offline
        D Offline
        DamienL
        wrote on last edited by
        #3

        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
        0

        • Login

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