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. How to merge/append/add two models for threading ?
Forum Updated to NodeBB v4.3 + New Features

How to merge/append/add two models for threading ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.2k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Dogukan
    wrote on 4 Sept 2018, 10:25 last edited by
    #1

    Hey Guys !!!
    I am kinda newbie so if this post is in wrong place just inform me please.

    I am trying to use threads for "for loops" in my program but according to my research "model->setData" is not compatible with threading.

    So my solutions is :
    -> I am gonna use different models in each thread and at the and I am gonna merge them into one to show in tableview.

    But I am not familiar with qt so I kinda stuck here could you please check my code ?

    t2 = std::thread{[&]{
    
                                    for(unsigned int i = 0 ; i < ((RegexOperations_.indexed_arranged_file.size()+1) / 2)  ; i++)
                                    {
    
                                        for(unsigned int j = 0 ; j < RegexOperations_.indexed_arranged_file[0].size();j++)
                                        {
                                            std::string temp = RegexOperations_.indexed_arranged_file[i][j];
                                            QModelIndex index = model ->index(i,j,QModelIndex());
                                            model->setData(index,temp.c_str());
                                        }
    
                                    }
    
                                }
                            };
    
    //                        t3 = std::thread{[&]{
    
    //                                for(unsigned int i = ((RegexOperations_.indexed_arranged_file.size()+1) / 2) ; i < RegexOperations_.indexed_arranged_file.size();i++)
    //                                {
    //                                    for(unsigned int j = 0 ; j < RegexOperations_.indexed_arranged_file[0].size();j++)
    //                                    {
    //                                        std::string temp = RegexOperations_.indexed_arranged_file[i][j];
    //                                        QModelIndex index = model ->index(i,j,QModelIndex());
    //                                        model->setData(index,temp.c_str());
    //                                    }
    //                                }
    //                            }
    //                        };
                        }
    
    
    
                t2.join();
                //t3.join();
    
                ui->tableView_results->setModel(model);
                ui->tableView_results->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
                ui->tableView_results->setEditTriggers(QAbstractItemView::NoEditTriggers);
                }
    

    Thank you for your help ...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Sept 2018, 20:54 last edited by
      #2

      Hi and welcome to devnet,

      Why not use Qt's Signal and Slots ? That way you can have only one model that's updated by two threads.

      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
      0
      • D Offline
        D Offline
        Dogukan
        wrote on 5 Sept 2018, 08:23 last edited by
        #3

        I did a little research about signals and slots but could not figure out how to implement into my code. Could you give me a little detailed explanation ?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 5 Sept 2018, 09:05 last edited by VRonin 9 May 2018, 12:18
          #4

          The big question is: is the model flat? i.e. is it a list/table or a tree?

          If so it's easy:

          class ModelUpdateNotify : public QObject{
          Q_OBJECT
          Q_DISABLE_COPY(ModelUpdateNotify)
          public:
          explicit ModelUpdateNotify(QObject* parent = Q_NULLPTR) : QObject(parent){}
          Q_SIGNAL void updateData(int row, int column, const QVariant& data);
          };
          
          ModelUpdateNotify* notifier2 = new  ModelUpdateNotify;
          QObject::connect(notifier2, &ModelUpdateNotify::updateData,model,[=](int row, int column, const QVariant& data){
          model->setData(model ->index(i,j),data);
          },Qt::QueuedConnection);
          t2 = std::thread{[&,notifier2]{
              for(unsigned int i = 0 ; i < ((RegexOperations_.indexed_arranged_file.size()+1) / 2)  ; i++){
                  for(unsigned int j = 0 ; j < RegexOperations_.indexed_arranged_file[0].size();j++)
                      notifier2->updateData(i,j,QString::fromStdString(RegexOperations_.indexed_arranged_file[i][j]));
              }
          delete notifier2;
          };
          

          "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

          1 Reply Last reply
          2
          • D Offline
            D Offline
            Dogukan
            wrote on 5 Sept 2018, 10:30 last edited by
            #5

            my model is QTableViewModel

            J V 2 Replies Last reply 5 Sept 2018, 10:59
            0
            • D Dogukan
              5 Sept 2018, 10:30

              my model is QTableViewModel

              J Offline
              J Offline
              JonB
              wrote on 5 Sept 2018, 10:59 last edited by
              #6

              @Dogukan
              There is no such Qt class as "QTableViewModel"... ?

              1 Reply Last reply
              1
              • D Dogukan
                5 Sept 2018, 10:30

                my model is QTableViewModel

                V Offline
                V Offline
                VRonin
                wrote on 5 Sept 2018, 12:18 last edited by
                #7

                @Dogukan said in How to merge/append/add two models for threading ?:

                my model is QTableViewModel

                It's not really relevant, any flat QAbstractItemModel would work with the method above

                "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

                1 Reply Last reply
                2

                1/7

                4 Sept 2018, 10:25

                • Login

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