Important: Please read the Qt Code of Conduct -

Issue with my QList that I am trying to set as a model for a QListView.

  • I have created a custom QObject that looks like this

    class Budget : public QObject
        Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
        Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)

    I am using this object to save the results of a query in a list as a QList<QObject *> and I then return it to my qml object.

    while ( {
        Budget *budget = new Budget();

    I have created my list view like this:

    ListView {
        id: budgetListView
        delegate: ListItem.Subtitled {
            text: name
            subText: description
            valueText: value
            maximumLineCount: 3

    I do not immediately define the model since I want it to be created when I enter some search results. For that I have a button that when pressed will return the list from my c++ function to the ListView's model:

    budgetListView.model = MyApp.ListBudget(nameTextField.text ...

    The thing is though that after I press the button I get a message that the various variables like name and description are not defined.

    Why is this and how can I fix it?

  • Lifetime Qt Champion


    Because name, descriptionand value here are considered as roles of your models, not the properties of your objects. So you are not accessing your objects directly but the model.

  • Hi SGaist,

    I tried changing the ListView so it looks like this:

    ListView {
            id: budgetListView
            delegate: ListItem.Subtitled {
                subText: modelData.description
                valueText: modelData.value
                maximumLineCount: 3

    Something that does indeed remove the error messages, but after pressing the search button nothing really happens. I know the values are passed correctly since I can print them with something like console.log(budgetListView.model[0].name);. So I am not sure if it is possible to assign the model to my ListView later on (in this case at the press of a button), and if that List will update itself.

  • Lifetime Qt Champion

    How are you changing the model ?

  • @SGaist I am not sure how should I update it. What I do until now is have a button that calls a method located in a javascript file that calls the final query on my c++ class that returns a list (Sounds weird when I write it down).


    Button {
        id: registerButton
        text: "Search"
        textColor: Theme.primaryColor
        enabled: true
        onClicked: FormChecks.getBudgetList()


    function getBudgetList()
       budgetList = MyEngine.ListBudget(nameTextField.text, nameModifier.selectedText, ...

    I connect qml and c++ by using

    qmlRegisterSingletonType<MyEngine>("AppManager", 0, 1, "MyEngine", MyEngine::qmlSingleton)

    budgetList is what I use as the model for my ListView.

    Im not sure that the ListView knows that the model is updated though.

  • Lifetime Qt Champion

    Indeed, it's not really clear what you are doing with budgetList.

    Out of curiosity, why do you create a new model ?

  • It seemed quite logical at the time. I wanted to display the results of a query (that is saved as a QList in C++) into a QListView in QML, thus every time the Search button is pressed I get a new QList that I try to pass as a model for my QListView.

  • Lifetime Qt Champion

    ListView is a QML component it's not a QListView.

    In any case, I'd recommend implementing a model where you'll be able to execute these queries and modify the model itself like described here. You can even use a QSortFilterProxyModel if you need filtering.

  • I had a look at the abstractitemmodel example when I was starting to do the first tests but I dont think that it can work for me due to the way I connect qml with c++. If it would make it clearer then please have a look at the actual code here. The files in question are databasehandler.cpp in Engine and ListBudgetDemo.qml in App.

  • Lifetime Qt Champion

    Since you are interfacing a SQL database, why not make use of the dedicate models ? That would simplify things.

Log in to reply