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. QThread operates QAbstractTableModel but no database, is it safe?
Qt 6.11 is out! See what's new in the release blog

QThread operates QAbstractTableModel but no database, is it safe?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 511 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.
  • Q Offline
    Q Offline
    QtTester
    wrote on last edited by
    #1

    Hi,all.
    I have a tableView , binded with a QAbstractTableModel, but NO database required.
    The data will be caculated in a complicated way so it takes much time to be loaded.

    When caculating data in UI thread, the tableView will be stuck for a while.

    So I move the caculating to a Qthread ,and in the thread :

    {
    // caculate things
    for(...){
        caculate();
        m_parent->m_tableModel->setItem(r,c,item);
    }
    // do other thing and finally set header:
    m_parent->m_tableModel->setHorizontalHeaderLabels(strListH);
    }
    

    where m_parent is the UI class, and m_tableModel is QAbstractTableModel.
    Under this operation ,the TableView does not be stuck again.

    But ,I wonder if this operation is safe under thread to call tableModel function?

    If NOT SAFE, how to solve the UI stuck problem?

    Thanks.

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

      You must not access/modify ui elemtns from another thread than the main gui thread which you do when you add/remove/modify a model.
      Use signals and slots to transfer the data from the worker thread to your model.

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

      Q 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        You must not access/modify ui elemtns from another thread than the main gui thread which you do when you add/remove/modify a model.
        Use signals and slots to transfer the data from the worker thread to your model.

        Q Offline
        Q Offline
        QtTester
        wrote on last edited by QtTester
        #3

        @Christian-Ehrlicher
        What if I first caculate and set tableModel in work Thread, then Bind tableModel in UI thread?Just Like:

        tableModel = new QStandardItemModel(row,col,this);
        
        waitThreadCaculateDone(); // fill tableModel data , with EventLoop so ui won't stuck
        tableView.bind(tableModel); // bind at the end
        
        JonBJ 1 Reply Last reply
        0
        • Q QtTester

          @Christian-Ehrlicher
          What if I first caculate and set tableModel in work Thread, then Bind tableModel in UI thread?Just Like:

          tableModel = new QStandardItemModel(row,col,this);
          
          waitThreadCaculateDone(); // fill tableModel data , with EventLoop so ui won't stuck
          tableView.bind(tableModel); // bind at the end
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @QtTester
          I assume you mean you pass tableModel to your blocking thread for filling purposes. Anyway the code you show on its own looks OK to me.

          Q 1 Reply Last reply
          0
          • JonBJ JonB

            @QtTester
            I assume you mean you pass tableModel to your blocking thread for filling purposes. Anyway the code you show on its own looks OK to me.

            Q Offline
            Q Offline
            QtTester
            wrote on last edited by
            #5

            @JonB Thanks , we test a lot, not broken so far :-)

            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