Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Manually refresh QListWidget while inserting

Manually refresh QListWidget while inserting

Scheduled Pinned Locked Moved Solved General and Desktop
qlistwidget
12 Posts 3 Posters 8.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.
  • M Offline
    M Offline
    Max13
    wrote on 20 Jul 2016, 15:16 last edited by
    #1

    I'm currently adding a huge list of items in my QListWidget, the process takes approximately from 5 to 10 secs.

    I would like to manually refresh the new items in the widget to show the "progress", how can I efficiently do it? Apparently, calling repaint() and udpate refreshes all the items, so it takes more time as there is more items, and the widget refreshes itself when it comes back to the event loop.

    While I'm adding the items to the list, I'm using a recursive call, so I won't return to the event loop before the end.

    If possible, I would like to add an item and refresh the last item to be shown, and so on (or maybe each 5 items, for instance).

    Thank you

    We all have started by asking questions. Then after some time, we can begin answering them.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 20 Jul 2016, 15:46 last edited by
      #2

      You can simply call QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      M 1 Reply Last reply 20 Jul 2016, 15:57
      1
      • V VRonin
        20 Jul 2016, 15:46

        You can simply call QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);

        M Offline
        M Offline
        Max13
        wrote on 20 Jul 2016, 15:57 last edited by
        #3

        @VRonin Thank you for the hint. It works almost as expected. The scrollbar shrinks but the widget stays empty. But when I focus on another app or refocus on mine, the content on the widget flashes and disappears again.

        Is it related to the speed of insertion? Is there a last thing missing?

        We all have started by asking questions. Then after some time, we can begin answering them.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 20 Jul 2016, 22:15 last edited by SGaist
          #4

          Hi,

          How many items are you trying to add ?

          In any case, it looks like you should rather consider QListView with a custom QAbstractListModel.

          Doing so you will be able to implement smarter algorithm to handle injection of big amount of data like batch insertion maybe a bit of thread to offload the work from the main thread etc.

          Hope it helps

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

          M 1 Reply Last reply 21 Jul 2016, 00:44
          2
          • S SGaist
            20 Jul 2016, 22:15

            Hi,

            How many items are you trying to add ?

            In any case, it looks like you should rather consider QListView with a custom QAbstractListModel.

            Doing so you will be able to implement smarter algorithm to handle injection of big amount of data like batch insertion maybe a bit of thread to offload the work from the main thread etc.

            Hope it helps

            M Offline
            M Offline
            Max13
            wrote on 21 Jul 2016, 00:44 last edited by Max13
            #5

            @SGaist Thanks for the suggestion. Currently, I'm adding as much directories and files in a selected folder on the computer. So it can be from 1 to ... elements.

            Before subclassing all that, I made another class which receives the QListWidget and has a crawl method. When crawling, I tried to call: QtConcurrent::run(crawler, &Crawler::crawl), which works correctly but still freezes the GUI. Is that what you suggested?

            EDIT: BTW, I'm adding them by creating QListWidgetItems 1 by 1.

            We all have started by asking questions. Then after some time, we can begin answering them.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Max13
              wrote on 21 Jul 2016, 12:53 last edited by
              #6

              I was using QFuture<T>::waitForFinished() which was blocking the main thread. I now send a signal when it's finished and return peacefully to the main loop.

              I'm still unable to see the changes in the list, live. Doing a refresh (using refresh or QApplication::processEvents()) will refresh all the list, is it possible to only "commit" the last added element?

              We all have started by asking questions. Then after some time, we can begin answering them.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 21 Jul 2016, 20:33 last edited by
                #7

                Why not use a QFileSystemModel ?

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

                M 1 Reply Last reply 21 Jul 2016, 21:04
                1
                • S SGaist
                  21 Jul 2016, 20:33

                  Why not use a QFileSystemModel ?

                  M Offline
                  M Offline
                  Max13
                  wrote on 21 Jul 2016, 21:04 last edited by
                  #8

                  @SGaist Because I think it would be more expensive to use this model then parse it searching for what I have to hide. So I thought, it would be faster to just recursively parse the filesystem myself and immediately sort what I need to keep or hide. Isn't it?

                  We all have started by asking questions. Then after some time, we can begin answering them.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 21 Jul 2016, 22:28 last edited by
                    #9

                    You can add custom QSortFilterProxyModel that will remove what you don't want.

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

                    M 1 Reply Last reply 22 Jul 2016, 14:41
                    1
                    • S SGaist
                      21 Jul 2016, 22:28

                      You can add custom QSortFilterProxyModel that will remove what you don't want.

                      M Offline
                      M Offline
                      Max13
                      wrote on 22 Jul 2016, 14:41 last edited by
                      #10

                      @SGaist Actually, "sorting" here means I have to exclude considering characteristics, like filename length. For real, is it heavier to feed the widget myself, than using the Model, View and sorter?

                      If you say so, as a forgotten Qt Ambassador I can only trust a permanent Qt Champion ;)

                      But I still would like to know what's the problem with my code. Anyway, I will look for the issue later

                      We all have started by asking questions. Then after some time, we can begin answering them.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 22 Jul 2016, 22:13 last edited by
                        #11

                        It's rather the filter part that is interesting in that class for your use case.

                        Not knowing your code I can't comment ;)

                        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
                        1
                        • M Offline
                          M Offline
                          Max13
                          wrote on 30 Jul 2016, 10:52 last edited by
                          #12

                          Finally... After few docs and experiences, I don't know how but I missed the fact that QWidgets are not reentrant (thanks to @kshegunov), and I also missed the fact that signals and slots are made for that.

                          @SGaist So, thanks for your advices but doing real MVC made my app too complicated for what it does. In a more constructed project, I would do what you suggested. I've simplified mine:

                          1. Choose your "root" directory
                          2. The Crawler crawls (recursively in Qt::Concurrent) and send a signal when the QFileInfo::fileName().length() is more than x characters
                          3. A slot in the MainWidget adds the item in the QListWIdget

                          That's all for now (auto-rename for later), so few lines of code. Thank you for your advices.

                          We all have started by asking questions. Then after some time, we can begin answering them.

                          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