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. QML ListView freezes with large models - creates and destroys all items

QML ListView freezes with large models - creates and destroys all items

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqt6qt6.4.0listview
12 Posts 3 Posters 1.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 3 Jan 2023, 15:31 last edited by
    #1

    When I supply a ListView with a very large model, like 500 or more items, it creates the delegates for all 500+ items and subsequently removes them, causing the application to freeze for a while.

    I thought ListViews were only supposed to create the delegates that would be visible, to allow large models to be loaded without slowing down the program? or is it only meant to do that while scrolling, and not creating & loading the list?

    It does that with a simple listview like this:

    ListView {
        id: list
        model: listModel
        spacing: 10
        anchors.fill: parent
        delegate: Rectangle {
            color: "black"
            width: 40
            height: 40
            Component.onCompleted: {
                console.log("create list item");
            }
            Component.onDestruction: {
                console.log("destroy list item");
            }
        }
    }
    

    8b41464c-285d-46bf-9d83-bb70b35e0c3c-image.png

    The model is a JavaScript object, if that's relevant at all.

    1 Reply Last reply
    0
    • J jeremy_k
      3 Jan 2023, 17:58

      What version of Qt and platform is this occurring on?

      Try printing the index, or another unique identifier from the model items to determine if it is creating and destroying for every index, or repeatedly for the same subset.

      ? Offline
      ? Offline
      A Former User
      wrote on 3 Jan 2023, 18:08 last edited by A Former User 1 Mar 2023, 18:12
      #11

      @jeremy_k on Qt 6.4.0

      huh..

      (at the beginning)
      c39cd0da-943a-44a9-9226-e088786178aa-image.png

      (later)
      7381c671-543e-4488-8fda-d06bdc639723-image.png

      that's bizarre, it's just creating and destroying the same items over and over again
      (and when i supply it with a smaller model, it creates and destroys less and loads quicker)

      Oh, i'm setting the model several times, that's why, one time for every single item
      it should only be setting the model once, i'll have to go see where else it's setting it
      ok i can work from here on my own

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JoeCFD
        wrote on 3 Jan 2023, 15:50 last edited by JoeCFD 1 Mar 2023, 15:51
        #2

        measure time to check if addition and deletion are too fast. If yes, add a timer to add them one after another smoothly.

        ? 1 Reply Last reply 3 Jan 2023, 17:09
        0
        • J JoeCFD
          3 Jan 2023, 15:50

          measure time to check if addition and deletion are too fast. If yes, add a timer to add them one after another smoothly.

          ? Offline
          ? Offline
          A Former User
          wrote on 3 Jan 2023, 17:09 last edited by
          #3

          @JoeCFD said in QML ListView freezes with large models - creates and destroys all items:

          measure time to check if addition and deletion are too fast. If yes, add a timer to add them one after another smoothly.

          You mean slowly add to the model rather than just setting the model? That could help, but it doesn't really answer my question

          like i should be able to just set the model
          model = <big array>
          listviews are supposed to only load items inside the visible area

          and i don't get what you mean by "measure time to check if addition and deletion are too fast. " measure what time?

          J 1 Reply Last reply 3 Jan 2023, 17:17
          0
          • ? A Former User
            3 Jan 2023, 17:09

            @JoeCFD said in QML ListView freezes with large models - creates and destroys all items:

            measure time to check if addition and deletion are too fast. If yes, add a timer to add them one after another smoothly.

            You mean slowly add to the model rather than just setting the model? That could help, but it doesn't really answer my question

            like i should be able to just set the model
            model = <big array>
            listviews are supposed to only load items inside the visible area

            and i don't get what you mean by "measure time to check if addition and deletion are too fast. " measure what time?

            J Offline
            J Offline
            JoeCFD
            wrote on 3 Jan 2023, 17:17 last edited by JoeCFD 1 Mar 2023, 17:18
            #4

            @eramne you measure the time between two additions or deletions or one addition and one deletion.
            Remember that each addition or deletion will trigger update of the GUI. Cache all data into a string list and add them slowly one after another with a timer when needed. I do not know how your data is generated.

            ? 1 Reply Last reply 3 Jan 2023, 17:24
            0
            • J JoeCFD
              3 Jan 2023, 17:17

              @eramne you measure the time between two additions or deletions or one addition and one deletion.
              Remember that each addition or deletion will trigger update of the GUI. Cache all data into a string list and add them slowly one after another with a timer when needed. I do not know how your data is generated.

              ? Offline
              ? Offline
              A Former User
              wrote on 3 Jan 2023, 17:24 last edited by
              #5

              @JoeCFD but what does the speed of created delegates have to do with my problem?

              let me restate my problem - i'm trying to set the list's model to a large model; the list view should create only the delegates that would be inside the visible area, but instead it creates delegates for all the items in the model and deletes them after, leaving the visible area. But it should only create the delegates that are needed, and ignore ones outside the visible area, and i thought that's what list views were supposed to do. I thought this whole system of loading items in the view and destroying ones outside the view was meant to allow large models in listviews? is this a bug, can i work around it?

              J 1 Reply Last reply 3 Jan 2023, 17:30
              0
              • ? A Former User
                3 Jan 2023, 17:24

                @JoeCFD but what does the speed of created delegates have to do with my problem?

                let me restate my problem - i'm trying to set the list's model to a large model; the list view should create only the delegates that would be inside the visible area, but instead it creates delegates for all the items in the model and deletes them after, leaving the visible area. But it should only create the delegates that are needed, and ignore ones outside the visible area, and i thought that's what list views were supposed to do. I thought this whole system of loading items in the view and destroying ones outside the view was meant to allow large models in listviews? is this a bug, can i work around it?

                J Offline
                J Offline
                JoeCFD
                wrote on 3 Jan 2023, 17:30 last edited by
                #6

                @eramne I guess list view will create as many delegates as the size of your model. If you add any new to the model, a new delegate will be created. You need to know if you are replacing an item or adding an item to the model.

                ? 1 Reply Last reply 3 Jan 2023, 17:37
                0
                • J JoeCFD
                  3 Jan 2023, 17:30

                  @eramne I guess list view will create as many delegates as the size of your model. If you add any new to the model, a new delegate will be created. You need to know if you are replacing an item or adding an item to the model.

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 3 Jan 2023, 17:37 last edited by
                  #7

                  @JoeCFD

                  You need to know if you are replacing an item or adding an item to the model.

                  i'm setting the model to a large javascript array

                  I guess list view will create as many delegates as the size of your model. If you add any new to the model, a new delegate will be created.

                  but it shouldn't, that's what i'm saying
                  it should only need to create what's visible
                  instead, it creates all of them, and destroys everything except what's visible
                  its a redundant middle step that's causing it to freeze
                  i'm asking, is this a bug with the ListView? can i work around it?

                  J 1 Reply Last reply 3 Jan 2023, 17:41
                  0
                  • ? A Former User
                    3 Jan 2023, 17:37

                    @JoeCFD

                    You need to know if you are replacing an item or adding an item to the model.

                    i'm setting the model to a large javascript array

                    I guess list view will create as many delegates as the size of your model. If you add any new to the model, a new delegate will be created.

                    but it shouldn't, that's what i'm saying
                    it should only need to create what's visible
                    instead, it creates all of them, and destroys everything except what's visible
                    its a redundant middle step that's causing it to freeze
                    i'm asking, is this a bug with the ListView? can i work around it?

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 3 Jan 2023, 17:41 last edited by
                    #8

                    @eramne I could be wrong. Simply try your code with large size in your model to see how many delegates are created(you have output messages). I use C++ code with Qt StringListModel as model to display debug messages and did not see any freezing issue.

                    ? 1 Reply Last reply 3 Jan 2023, 17:55
                    0
                    • J JoeCFD
                      3 Jan 2023, 17:41

                      @eramne I could be wrong. Simply try your code with large size in your model to see how many delegates are created(you have output messages). I use C++ code with Qt StringListModel as model to display debug messages and did not see any freezing issue.

                      ? Offline
                      ? Offline
                      A Former User
                      wrote on 3 Jan 2023, 17:55 last edited by A Former User 1 Mar 2023, 17:56
                      #9

                      @JoeCFD said in QML ListView freezes with large models - creates and destroys all items:

                      @eramne I could be wrong. Simply try your code with large size in your model to see how many delegates are created(you have output messages).

                      yeah, i already did that, and said what i discovered was happening in the question and in the replies.
                      i logged when items are created and destroyed, and discovered that:

                      it should only need to create what's visible
                      instead, it creates all of them, and destroys everything except what's visible
                      its a redundant middle step that's causing it to freeze

                      i added an image in the first post too

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jeremy_k
                        wrote on 3 Jan 2023, 17:58 last edited by
                        #10

                        What version of Qt and platform is this occurring on?

                        Try printing the index, or another unique identifier from the model items to determine if it is creating and destroying for every index, or repeatedly for the same subset.

                        Asking a question about code? http://eel.is/iso-c++/testcase/

                        ? 1 Reply Last reply 3 Jan 2023, 18:08
                        0
                        • J jeremy_k
                          3 Jan 2023, 17:58

                          What version of Qt and platform is this occurring on?

                          Try printing the index, or another unique identifier from the model items to determine if it is creating and destroying for every index, or repeatedly for the same subset.

                          ? Offline
                          ? Offline
                          A Former User
                          wrote on 3 Jan 2023, 18:08 last edited by A Former User 1 Mar 2023, 18:12
                          #11

                          @jeremy_k on Qt 6.4.0

                          huh..

                          (at the beginning)
                          c39cd0da-943a-44a9-9226-e088786178aa-image.png

                          (later)
                          7381c671-543e-4488-8fda-d06bdc639723-image.png

                          that's bizarre, it's just creating and destroying the same items over and over again
                          (and when i supply it with a smaller model, it creates and destroys less and loads quicker)

                          Oh, i'm setting the model several times, that's why, one time for every single item
                          it should only be setting the model once, i'll have to go see where else it's setting it
                          ok i can work from here on my own

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jeremy_k
                            wrote on 3 Jan 2023, 18:19 last edited by
                            #12

                            Defining a handler for a property change signal, and then setting a break point in that handler is my usual technique for finding unwanted changes.

                            Asking a question about code? http://eel.is/iso-c++/testcase/

                            1 Reply Last reply
                            0

                            1/12

                            3 Jan 2023, 15:31

                            • Login

                            • Login or register to search.
                            1 out of 12
                            • First post
                              1/12
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved