Duration of aThread



  • Hello,
    I want know how I can get duration of a thread which I run in my program.
    Thanks for advance



  • If you want to know how long your thread runs, may be you can use a timer inconjunction with the signal finished(), started() and terminated() to measure.



  • it depends of thread implementation and required accuracy.
    The easiest qt-way is to create instance of QTime within thread and then use QTime::start() & QTime::elapsed.



  • Do you want to measure the time or do you want to know when a thread terminates? It terminates when QThread::run() has completed. Please give more details about what you are trying to achieve if the existing posts do not answer your question.



  • Here it is the Object which works..
    @
    class Worker :public QObject
    {

    public:
    void setModel( QAbstractItemModel *model)
    {
    m_model= model;
    }

    QAbstractItemModel *model()
    {
        return m_model;
    }
    

    private:
    QAbstractItemModel *m_model;

    signals:

    public slots:

      void doWork()
      {
    
          //Extraction de la matrice
          int n=m_model->rowCount()-3;
          double  *mat=new double[n*n];
          double  *matInv=new double[n*n];
    
          for( int i=3,r=0; i < m_model->rowCount(); ++i ,++r)
          {
              for(int j=3,c=0; j < m_model->columnCount(); ++j,++c)
              {
    
                  if(m_model->data(m_model->index(i,j)).isValid()){
    
                      mat[r+c*n]= m_model->data(m_model->index(i,j)).toDouble();
                  }else
                  {
                      mat[r+c*n]=0.;
                  }
    
              }
    
          }
    
    
          //Inversion de la matrice
            InverseMatrix *inverser=new  InverseMatrix;
            inverser->gaussR(mat,matInv,n);
    
            //Retablissement de la matrice
            for( int i=3,r=0; i <  m_model->rowCount(); ++i,++r )
            {
    
                for(int j=3,c=0; j < m_model->columnCount(); ++j,++c)
                {
    
                    if( m_model->data(m_model->index(i,j)).isValid() ){
    
                        if( matInv[r+c*n] !=0.0000)
                            m_model->setData(m_model->index(i,j),QString::number(matInv[r+c*n],'f',4));
                        else
                            m_model->setData(m_model->index(i,j),QString());
                            // m_model->,->setBackground(QBrush(QColor(255,241,113)));
    
                    }else
                    {
             
                        if( matInv[r+c*n] !=0.0000)
                            m_model->setData(m_model->index(i,j),QString::number(matInv[r+c*n],'f',4));
                        else
                             m_model->setData(m_model->index(i,j),QString());
                    }
    
                }
    
            }
    
      }
    

    };

    @

    Now i call it from an another Object cross a thread
    @
    ScrollAreaInverseMatrix::ScrollAreaInverseMatrix(QWidget *parent) :
    QScrollArea(parent)
    {
    setupUi(this);
    setupContents();
    }

    void ScrollAreaInverseMatrix::setupContents()
    {

      ScrollAreaMatriceIC *matrixIc=new ScrollAreaMatriceIC;
    
      if( matrixIc->getTableWidget() !=0){
    
          table=matrixIc->getTableWidget();
    
           QTableWidgetItem *curItem;
          for( int i=3 ;i <  table->rowCount(); ++i)
          {
    
              for(int j=3; j < table->columnCount(); ++j)
              {
    
                  if( table->item(i,j) != 0){
                     table->item(i,j)->setBackground(QBrush(QColor(255,241,113)));
    
                  }else
                  {
                      curItem=new QTableWidgetItem;
                      curItem->setBackground(QBrush(QColor(255,241,113)));
                      table->setItem(i,j,curItem);
                  }
    
              }
          }
    
          QTableView *view= new QTableView;
          horizontalLayout->addWidget(view);
    
    
          Worker * worker=new Worker;
          worker->setModel(table->model());
          worker->doWork();
          QThread *thread=new QThread;
          worker->moveToThread(thread);
          thread->start(QThread::HighestPriority);
          view->setModel(worker->model());
    
          QMessageBox dialog(QMessageBox::Information,"","Finished");
          connect(thread,SIGNAL(terminated()),&dialog,SLOT(open()));
         //after my object is visible with results ,but dialog never shows..... 
      
    
    
            view->resizeColumnsToContents();
            view->resizeRowsToContents();
    
      }
    

    }
    @

    My aim is to show ProgressDialog which determines elapsed time before my Object become visible with my results(cause my object takes 4 seconds before it becomes visible).
    Thanks for advance.



  • In that case time is not interesting. You might want to emit a progressChanged() signal containing a percentage. Stick to what you know. You know exactly how many calculations you are going to do, but you have no real clue about how much time it will take to complete them. If you use a percentage output, you can use a standard QProgressDialog and use that. It will by default pop up after 4 seconds of operation, but you can change that value if you wish, or show() immediately.

    People are usually better at guessing or estimating (guestimating) the time to finish based on tasks-completed progress than computers are.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.