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. UI blocking during update from slot
Forum Updated to NodeBB v4.3 + New Features

UI blocking during update from slot

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.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.
  • N Offline
    N Offline
    Nubcake
    wrote on last edited by Nubcake
    #1

    I have a QTreeView which is paired with a QStandardItemModel and created in the main thread.
    I have a worker thread running which is processing a lot of data and uses a signal/slot to update the model in the main thread. The slot in the main thread just basically appends a new row of items to the model:

    void MainWindow::slotAppendRow() noexcept
    {
        QList<QStandardItem*> items;
        items.append(new QStandardItem("1"));
        items.append(new QStandardItem("2"));
        items.append(new QStandardItem("3"));
        items.append(new QStandardItem("4"));
        m_treeModel.appendRow(items);
    }
    

    I must be emitting about hundreds of signals every second and I'm noticing the interaction with the UI becomes really choppy/blocks sometimes while the slot is adding items to the model. I've tried to offload as much computation in the worker thread as possible but given that it's not possible to update the UI from a different thread in Qt, how can I make my GUI 100% responsive? I.e how can I prevent the GUI from blocking?

    MainWindow.h

    QStandardItemModel m_treeModel;
    

    MainWindow.cpp

    // Setup model
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ...
    m_ui->m_tree->setModel(&m_treeModel);
    

    Where m_tree is a QTreeView set in the form designer.

    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #2

      @Nubcake said in UI blocking during update from slot:
      How do you signal the view to update the change in the model?

      N 1 Reply Last reply
      0
      • nageshN nagesh

        @Nubcake said in UI blocking during update from slot:
        How do you signal the view to update the change in the model?

        N Offline
        N Offline
        Nubcake
        wrote on last edited by
        #3

        @nagesh I don't actually, it updates by itself it seems.

        1 Reply Last reply
        0
        • nageshN Offline
          nageshN Offline
          nagesh
          wrote on last edited by
          #4

          @Nubcake some more code snippet is required to answer what is wrong in GUI update.
          treeModel construction and setup with view.

          N 1 Reply Last reply
          0
          • nageshN nagesh

            @Nubcake some more code snippet is required to answer what is wrong in GUI update.
            treeModel construction and setup with view.

            N Offline
            N Offline
            Nubcake
            wrote on last edited by
            #5

            @nagesh Updated original post with the setup

            1 Reply Last reply
            0
            • nageshN Offline
              nageshN Offline
              nagesh
              wrote on last edited by
              #6

              @Nubcake confirm whether your computation and gui update is happening in different thread by printing threadid..

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Don't use a convenience model when you want to upgrade stuff that often. Also reduce the update rate - noone can see this in the view so it's not necessary.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                N 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  Don't use a convenience model when you want to upgrade stuff that often. Also reduce the update rate - noone can see this in the view so it's not necessary.

                  N Offline
                  N Offline
                  Nubcake
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher Could you explain what you mean by "convenience model"?
                  How often should I update the view would you suggest?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    QStandardItemModel is a convenience model optimized for general usage but not for mass (update) data. Use QAbstractItemModel instead.
                    I don't know how often you should update your ui - but 100 updates per second is for sure not needed and to much.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    N 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      QStandardItemModel is a convenience model optimized for general usage but not for mass (update) data. Use QAbstractItemModel instead.
                      I don't know how often you should update your ui - but 100 updates per second is for sure not needed and to much.

                      N Offline
                      N Offline
                      Nubcake
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher I've added QThread::msleep(50) just after emitting the signal to update the UI in the worker thread and it's more responsive as one would expect.

                      However I'm not fond of blocking the worker thread to allow the GUI to become responsive. Is there better way to limit the amount of signals I send to the UI without sleeping the worker thread?

                      JonBJ 1 Reply Last reply
                      0
                      • N Nubcake

                        @Christian-Ehrlicher I've added QThread::msleep(50) just after emitting the signal to update the UI in the worker thread and it's more responsive as one would expect.

                        However I'm not fond of blocking the worker thread to allow the GUI to become responsive. Is there better way to limit the amount of signals I send to the UI without sleeping the worker thread?

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @Nubcake
                        Emit less signals from the worker. Or, probably better, reduce the frequency with which you act on them in the UI thread, or batch some updates together, or similar.

                        1 Reply Last reply
                        2

                        • Login

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