Unable to assign ... to QQmlComponent



  • 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?



  • 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.



  • 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 !


Log in to reply
 

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