ListModel in external File



  • I would like to keep the List Model in an external file. I created the file CourseListModel.qml like this:

    ListModel {
        id: courseListModel
    
        ListElement {
            title: "Kurs 01"
            page: "content/ListPage.qml"
        }
        ListElement {
            title: "Kurs 02"
            page: "content/ListPage.qml"
        }
        ListElement {
            title: "Kurs 03"
            page: "content/ListPage.qml"
        }
    }
    

    In my other file CourseListPage.qml I have the ListView like this:

    ListView {
            anchors.fill: parent
            model: courseListModel
            /*
            model: ListModel {
                id: courseModel
                ListElement {
                    title: "Kurs 01"
                    page: "content/ListPage.qml"
                }
                ListElement {
                    title: "Kurs 02"
                    page: "content/ListPage.qml"
                }
                ListElement {
                    title: "Kurs 03"
                    page: "content/ListPage.qml"
                }
            }
            */
            delegate: AndroidDelegate {
                text: title
                onClicked: {
                    console.log("Course selected")
                    stackView.push("content/ListPage.qml")
                }
            }
        }
    

    When I run the program it get the following error:

    ReferenceError: courseListModel is not defined

    What am I doing wrong here



  • Hi @Infinity

    The problem is with this line : model: courseListModel

    Your syntax is wrong .

    The correct syntax should look to something like this :

    model: CourseListPage{}

    And if CourseListPage.qml is not on the same directory of your ListView , do not forget to add import CourseListPage

    Your ListView file should look to something like this:

    ListView {
            anchors.fill: parent
            model: CourseListPage{}
         .
         .
         .
        }
    

    I hope this can help you !


Log in to reply
 

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