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 using multi thread on this sample?

How using multi thread on this sample?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 1.9k 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.
  • T Offline
    T Offline
    tmtt66
    wrote on last edited by
    #1

    Hi
    how using multi thread on this sample?
    and two table on this sample fill asynchronous.

    "sample":http://tmtt66.persiangig.com/sample-multithread.rar

    thanks

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Please post the relevant code here.

      (Z(:^

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Huh, your like is in persian language. That's a bit hard to read ;-) Also like slerdzio says, post the code you don't understand. Then we might be able to help.

        Greetz, Jeroen

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tmtt66
          wrote on last edited by
          #4

          mainwindow
          @#include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include "mythread.h"
          #include <QtConcurrent/QtConcurrentRun>
          #include <QMessageBox>
          #include <qscrollbar.h>

          MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);
          ui->tbltest1->setColumnCount(1);
          ui->tbltest2->setColumnCount(1);
          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::on_btnstart_clicked()
          {
          ui->tbltest1->setRowCount(0);
          ui->tbltest2->setRowCount(0);

          threadtest1 = NULL;
          threadtest2 = NULL;
          
          threadtest1 = new mythread;
          threadtest2 = new mythread;
          
          connect(threadtest1, SIGNAL(updatetable1()), this, SLOT(runonthred1()));
          connect(threadtest2, SIGNAL(updatetable2()), this, SLOT(runonthred2()));
          
          threadtest1->name = "t1";
          threadtest2->name = "t2";
          
          threadtest1->start();
          threadtest2->start();
          

          }

          void MainWindow::runonthred1()
          {
          QtConcurrent::run(this, &MainWindow::setdatatoview1);
          }

          void MainWindow::runonthred2()
          {
          QtConcurrent::run(this, &MainWindow::setdatatoview2);
          }

          void MainWindow::setdatatoview1()
          {
          for (int var = 0; var < 100; ++var)
          {
          ui->tbltest1->setRowCount(ui->tbltest1->rowCount() + 1);
          ui->tbltest1->verticalScrollBar()->setSliderPosition(ui->tbltest1->verticalScrollBar()->maximum());

              threadtest1->msleep(100);
              QTableWidgetItem *data = NULL;
              data = new QTableWidgetItem(QString("test"));
              if(data)
              {
                  ui->tbltest1->setItem(var, 0, data);
              }
              else
              {
                  QMetaObject::invokeMethod(this, "UIStatus",  Qt::QueuedConnection, Q_ARG(bool, true));
                  return;
              }
          }
          

          }

          void MainWindow::setdatatoview2()
          {
          for (int var = 0; var < 100; ++var)
          {
          ui->tbltest2->setRowCount(ui->tbltest2->rowCount() + 1);
          ui->tbltest2->verticalScrollBar()->setSliderPosition(ui->tbltest2->verticalScrollBar()->maximum());

              threadtest2->msleep(10);
              QTableWidgetItem *data = NULL;
              data = new QTableWidgetItem(QString("test"));
              if(data)
              {
                  ui->tbltest2->setItem(var, 0, data);
              }
              else
              {
                  QMetaObject::invokeMethod(this, "UIStatus",  Qt::QueuedConnection, Q_ARG(bool, true));
                  return;
              }
          }
          

          }
          @

          thread
          @#include "mythread.h"
          #include <QDebug>
          #include <QtCore>
          #include "mainwindow.h"
          #include <QTimer>

          mythread::mythread(QObject *parent) :
          QThread(parent)
          {

          }

          void mythread::run()
          {
          if(this->name == "t1")
          {
          emit updatetable1();
          }
          if(this->name == "t2")
          {
          emit updatetable2();
          }
          }

          @
          @
          #ifndef MYTHREAD_H
          #define MYTHREAD_H

          #include <QThread>
          #include <QTimer>

          class mythread : public QThread
          {
          Q_OBJECT
          public:
          explicit mythread(QObject parent = 0);
          QString name;
          QTimer
          timer ;

          protected:
          void run();

          signals:
          void updatetable1();
          void updatetable2();

          public slots:
          };

          #endif // MYTHREAD_H
          @

          thanks a lot.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            Hi, please note that

            bq. QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tmtt66
              wrote on last edited by
              #6

              What is your recommend to solve problem?
              Please help me .
              thanks.

              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