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. Duration of aThread
Forum Updated to NodeBB v4.3 + New Features

Duration of aThread

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 2.7k 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.
  • M Offline
    M Offline
    maxoreli
    wrote on 1 Jul 2011, 07:42 last edited by
    #1

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

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rammi
      wrote on 1 Jul 2011, 07:55 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        romankr
        wrote on 1 Jul 2011, 08:24 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dialingo
          wrote on 1 Jul 2011, 08:42 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxoreli
            wrote on 1 Jul 2011, 08:57 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on 1 Jul 2011, 09:45 last edited by
              #6

              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.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0

              2/6

              1 Jul 2011, 07:55

              topic:navigator.unread, 4
              • Login

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