How using multi thread on this sample?



  • 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


  • Moderators

    Please post the relevant code here.



  • 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.



  • 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.



  • Hi, please note that

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



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


Log in to reply
 

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