1

I want to use comboBox which it's model is taken from a nested model.

The simplified Code:

main.qml:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: applicationWindow

    width: 300
    height: 200
    visible: true
    title: qsTr("01_Change_Model_Data")

    ListModel {
        id: listModel1
        ListElement {
            labelText: "ComboBox 1:"

            //subItems: ["First", "Second", "Third"]
            subItems: [
                ListElement {text: "First"},
                ListElement {text: "Second"},
                ListElement {text: "Third"}
            ]

        }
        ListElement {
            labelText: "ComboBox 2:"

            //subItems: ["First", "Second", "Third"]
            subItems: [
                ListElement {text: "First"},
                ListElement {text: "Second"},
                ListElement {text: "Third"}
            ]

        }
    }


    Button {
        id: loadUnloadBtn
        height: 24
        width: 50
        text: "Load"

        anchors {
            right: parent.right
            rightMargin: 20
            top: parent.top
            topMargin: 10
        }
        onClicked: {
            if(comboBoxAreaLoader.source == "") {
                comboBoxAreaLoader.source = "ComboBoxArea.qml"
            }else {
                comboBoxAreaLoader.source = ""
            }

        }
    }

    Loader {
        id: comboBoxAreaLoader
        anchors {
            top: parent.top
            topMargin: 10
            bottom: parent.bottom
            bottomMargin: 10
            left: parent.left
            leftMargin: 10
            right: parent.right
            rightMargin: 80
        }
        source: ""
        property variant comboBoxModel: subItems
        onStatusChanged: if(status == Loader.Ready) comboBoxModelAlias = comboBoxModel
    }
}

ComboBoxArea.qml:

import QtQuick 2.15
import QtQuick.Controls 2.15


Item {
    id: listViewDelegate
    ListView {
        id: listView1
        anchors.fill: parent

        model: listModel1
        delegate: listElementDelegate
        spacing: 6
    }

    Component {
        id: listElementDelegate
        Rectangle {
            id: delegateRectangleRoot

            property alias comboBoxModelAlias: comboBox.model

            height: 30
            width: 200
            Label {
                id: comboBoxNameLabel
                color: "red"
                width: 80
                height: 30
                anchors {
                    left: parent.left
                    leftMargin: 10
                }
                text: labelText
                verticalAlignment: Text.AlignVCenter
            }

            ComboBox {
                id: comboBox
                height: 30
                width: 100
                //model: ["First", "Second", "Third"]

                anchors {
                    left: comboBoxNameLabel.right
                    leftMargin: 10
                    verticalCenter: comboBoxNameLabel.verticalCenter
                }
            }

        }

    }
}

I think main problem is defining subItems in nested list. First, I tried to declare it as js list like:

subItems: ["First", "Second", "Third"]

But I got an error:

ListElement: cannot use script for property value

Then I tried to change it with listElements:

subitems: [
    ListElement {text: "First"},
    ListElement {text: "Second"},
    ListElement {text: "Third"}
]

This time I got two errors:

ReferenceError: subItems is not defined

Error: Invalid write to global property "comboBoxModelAlias"

Actually I'm confused. Do you have any idea what I am doing wrong?

Application window screenshot

3
  • You cannot randomly access undefined property from a Loader. you have to refer a least to the Loader.sourceItem component, but obviously only a component is loaded, which is not your case since your loader source is "" when declared Commented Jan 28, 2022 at 11:01
  • possibly duplication of stackoverflow.com/questions/31996279/… Commented Apr 26, 2022 at 8:16
  • Does this answer your question? Nested list in qml: data models in models Commented Apr 26, 2022 at 8:17

1 Answer 1

0

I think you were just making it a little more complex than it needed to be. By declaring subItems like below, they automatically become ListModels. And you can just refer to them in the delegate like you do labelText.

main.qml:

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: applicationWindow

    width: 300
    height: 200
    visible: true
    title: qsTr("01_Change_Model_Data")

    ListModel {
        id: listModel1
        ListElement {
            labelText: "ComboBox 1:"

            subItems: [
                ListElement {text: "First"},
                ListElement {text: "Second"},
                ListElement {text: "Third"}
            ]

        }
        ListElement {
            labelText: "ComboBox 2:"

            subItems: [
                ListElement {text: "First"},
                ListElement {text: "Second"},
                ListElement {text: "Third"}
            ]

        }
    }


    Button {
        id: loadUnloadBtn
        height: 24
        width: 50
        text: "Load"

        anchors {
            right: parent.right
            rightMargin: 20
            top: parent.top
            topMargin: 10
        }
        onClicked: {
            if(comboBoxAreaLoader.source == "") {
                comboBoxAreaLoader.source = "ComboBoxArea.qml"
            }else {
                comboBoxAreaLoader.source = ""
            }

        }
    }

    Loader {
        id: comboBoxAreaLoader
        anchors {
            top: parent.top
            topMargin: 10
            bottom: parent.bottom
            bottomMargin: 10
            left: parent.left
            leftMargin: 10
            right: parent.right
            rightMargin: 80
        }
    }
}

ComboBoxArea.qml:

import QtQuick 2.15
import QtQuick.Controls 2.15


Item {
    id: listViewDelegate
    ListView {
        id: listView1
        anchors.fill: parent

        model: listModel1
        delegate: listElementDelegate
        spacing: 6
    }

    Component {
        id: listElementDelegate
        Rectangle {
            id: delegateRectangleRoot

            height: 30
            width: 200
            Label {
                id: comboBoxNameLabel
                color: "red"
                width: 80
                height: 30
                anchors {
                    left: parent.left
                    leftMargin: 10
                }
                text: labelText
                verticalAlignment: Text.AlignVCenter
            }

            ComboBox {
                id: comboBox
                height: 30
                width: 100
                model: subItems

                anchors {
                    left: comboBoxNameLabel.right
                    leftMargin: 10
                    verticalCenter: comboBoxNameLabel.verticalCenter
                }
            }

        }

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.