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. Issue with my QList that I am trying to set as a model for a QListView.

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qlistview
10 Posts 2 Posters 3.1k Views
  • 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.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by
    #1

    I have created a custom QObject that looks like this

    class Budget : public QObject
    {
        Q_OBJECT
    
        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 (query.next()) {
        Budget *budget = new Budget();
        budget->setName(query.value("glb_name").toString());
        budget->setDescription(query.value("glb_description").toString());
    ...
    

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ealioneE Offline
        ealioneE Offline
        ealione
        wrote on last edited by
        #3

        Hi SGaist,

        I tried changing the ListView so it looks like this:

        ListView {
                id: budgetListView
                delegate: ListItem.Subtitled {
                    text: modelData.name
                    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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          How are you changing the model ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ealioneE Offline
            ealioneE Offline
            ealione
            wrote on last edited by
            #5

            @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

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

            Javascript

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

            C++
            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • ealioneE Offline
                ealioneE Offline
                ealione
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • ealioneE Offline
                    ealioneE Offline
                    ealione
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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