jeudi 9 janvier 2020

how to pass structure to QT thread

Am trying to pass data structure to QT thread and but no success.

here is what am doing and have done.

i prepare data for the thread, like this and then tried to pass prepared data to thread before starting.

void mytable::prepare_data(){
   // get table row count 
   int rowCount = ui->my_table_view->rowCount();    

  // create structure array  based on rowCount
  pnp_com_info pnp_data[rowCount];

  /* pnp_com_info structure defined it top of file below includes to make it global
    struct pnp_com_info{
      QString com_name = "";
      int x = 0;
      int y = 0;
      int angle = 0;
      bool status = false;
    };
  */

  // loop on table rows columns and load pnp_data with data of columns


// PROBLEM : how to pass pnp_data structure to thread side ?

 // can pass basic vars like
 RunJobThread->mynum = 10; 


// start QT thread 
RunJobThread->start();

 // std:: thread experiment
 // std::stdthreadtest(pnp_data,rowCount);

}

run_job_thread.h source code

#ifndef RUN_JOB_THREAD_H
#define RUN_JOB_THREAD_H

#include <QObject>
#include <QThread>

class run_job_thread : public QThread
{
    Q_OBJECT
public:
    run_job_thread();

    void run();

    int mynum;

        struct pnp_com_info_thread{
          QString com_name = "";
          int x = 0;
          int y = 0;
          int angle = 0;
          bool status = false;
        };
    bool Stop;  // bool to stop the job
signals:
    void select_row_of_table_signal(int);
public slots:

};
#endif // RUN_JOB_THREAD_H

run_job_thread.cpp source code

#include "run_job_thread.h"
#include <QtCore>

run_job_thread::run_job_thread()
{

}

// run the thread
void run_job_thread::run(){

  qDebug() << "my num passed value is : "<<this->mynum;  // output : 10

    // Goal : loop on pnp_data structure and emit signal to table rows

    emit select_row_of_table_signal(5);
}

things i tried

instead of struct i tried to use other data containers like map, multimap, vectors but they give error , as am initializing pnp_com_info struct inside mytable::prepare_data() function based on rowCount which make it local and limited to prepare_data() function but with map,multimap,vector my plan was that they will be global and i will be able to access it from thread, however it not worked.

std::map<std::string, int,int,int> pnp_com_info;  // error: too many template arguments for class template 'map'
std::multimap<std::string, int,int,int,bool> pnp_com_info; // error: too many template arguments for class template 'multimap'
std::vector<std::string, int,int,int,bool> pnp_com_info; // error: too many template arguments for class template 'vector'

i also tried std::thread which was partial success , i mean it was working ok but looks like std::thread not works with QT GUI thread as upon running app GUI will go freez although std::thread was doing its job

Aucun commentaire:

Enregistrer un commentaire