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. ListView model update
QtWS25 Last Chance

ListView model update

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 4 Posters 8.5k 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.
  • G Offline
    G Offline
    gogoer
    wrote on last edited by
    #1

    hello
    i have ListView that displays list of files in folder.

                        ListView{
                            model: fileChecker.getFilesCount(folderID);
                            delegate: Item{
                                    Button{
                                        anchors.fill: parent
                                        text: fileChecker.getFileName(index)
                                    }
                             }
                       }
    

    fileChecker class checks files in folder and returns their names.

    when window creates, it have list of files, everething is ok. but i need to update list if files appears or disappears in the folder. how can i update ListView?

    J.HilkJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      List view have property called model. Have you seen the ListModel methods ? like append, insert, delete ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • G gogoer

        hello
        i have ListView that displays list of files in folder.

                            ListView{
                                model: fileChecker.getFilesCount(folderID);
                                delegate: Item{
                                        Button{
                                            anchors.fill: parent
                                            text: fileChecker.getFileName(index)
                                        }
                                 }
                           }
        

        fileChecker class checks files in folder and returns their names.

        when window creates, it have list of files, everething is ok. but i need to update list if files appears or disappears in the folder. how can i update ListView?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        Hi @gogoer ,
        since fileChecker is a (c++?) class and you fetch your model via a function call, the model is only loaded once, during creation and no further updated.

        You have 2 options.

        • make it a proper Q_Property than your list view will update automatically
        • create a signal in your class that is emitted once the model changes and manually react to that signal in qml to reset the model
        //for example
        Connections{
            target: fileChecker
            onFilesCountChanged: myListView.model = fileChecker.getFilesCount(folderID)
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • G Offline
          G Offline
          gogoer
          wrote on last edited by
          #4

          i tryed to create function:

          function updateModel(){
             myListView.model = fileChecker.getFilesCount(folderID)
          }
          

          and call it by pressing button, but nothing changes. why?

          J.HilkJ 1 Reply Last reply
          0
          • Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on last edited by Pradeep P N
            #5

            Hi @gogoer

            Can you try using QAbstractListModel Class from QAbstractItemModel and expose it to QML.
            Where you can pass the class to Listview { model } and you get all the options from the C++ Class.
            Example

            Or
            As per my understanding in your present code you have to expose the update signal to QML so the model can be updated based on the signal.

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            1 Reply Last reply
            3
            • G gogoer

              i tryed to create function:

              function updateModel(){
                 myListView.model = fileChecker.getFilesCount(folderID)
              }
              

              and call it by pressing button, but nothing changes. why?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @gogoer said in ListView model update:

              updateModel

              are you sure updateModel is called ?
              are you sure fileChecker.getFilesCount actually returns a list with the new entries?

              function updateModel(){
                 console.log("Attempt to update model")
                 var newModel = fileChecker.getFilesCount(folderID);
                console.log("new Model", newModel) 
                 myListView.model = newModel
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              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