[Qt-interest] Using GUI to run QThread and Displaying output from QThread onto GUI QTableWidget

Wilson Pok wilson2785 at hotmail.com
Sat Sep 19 16:46:34 CEST 2009


I am trying to create a thread from my GUI, such that when a button is clicked, it runs the thread in the background and displays the information in QTableWidget in my GUI.

 

//mythread.cpp//

 

#include <QtGui/QMainWindow>

#include "mythread.h"

 

void MyThread::run()
{

    for(int i=0;i<10;i++)
    {

        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        QTableWidgetItem *rowItem = new QTableWidgetItem();
        rowItem->setText("Test");
        ui->tableWidget->setItem(row, 0, rowItem);
        
        sleep(3000);
    }

    exec();
}
 
When I compile, I get ther error "error: `ui' was not declared in this scope", the ui components are not expose to the thread. I tried to include "mainwindow.h" so as to expose the ui components but I get more errors instead because mainwindow.h includes mythread.h and mythread.h includes mainwindow.h. Thus I guess it does not work this way.
 
//mythread.h//

 

#ifndef SNIFFTHREAD_H

#define SNIFFTHREAD_H

 
#include <QtGui>
//#include "mainwindow.h"     (I tried to add this in to expose ui but i get more errors as I included mythread.h in mainwindow.h)

 
class MyThread : public QThread
{
    Q_OBJECT

public:
    MyThread(QObject *parent);
    //Ui::MainWindow *ui;      (I tried to add this in to expose ui but i get more errors as I included mythread.h in mainwindow.h)
    void run();
};
 

#endif // SNIFFTHREAD_H
 

======================================

 

How can I  create a thread from my GUI, such that when a button is clicked, it runs the thread in the background and displays the information in QTableWidget in my GUI??

_________________________________________________________________
See all the ways you can stay connected to friends and family
http://www.microsoft.com/windows/windowslive/default.aspx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090919/75ac28b3/attachment.html 


More information about the Qt-interest-old mailing list