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

Wilson Pok wilson2785 at hotmail.com
Sun Sep 20 06:34:38 CEST 2009


ok i get an rough idea. thanks. but can u provide some examples of connecting the signals? sorry I'm very new to QT. just some examples how i can connect my thread to link up with my GUI will do. Thanks!


> You could try a forward declaration of class Ui::MainWindow in mythread.h
> 
> Errors on your #include might be due to no include guards somewhere.
> Make sure you have those include guards! (#ifndef BLAH_H)
> 
> I have done something similar to what you're doing, and attempting to
> manipulate my GUI from a thread crashed my app though. I believe the
> proper way to do this is to use the signal/slot system. Define the
> signal in your thread class, the slot in your GUI class, and make the
> connection between the two.. probably at the time the thread is
> created.
> 
> Good luck!
> 
> Jimmy
> 
> 2009/9/19 Wilson Pok <wilson2785 at hotmail.com>:
> > 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??
> >
> > ________________________________
> > Buddy is 10! Create a comic strip of your ultimate party and win $1,000!
> > Click here
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at trolltech.com
> > http://lists.trolltech.com/mailman/listinfo/qt-interest
> >
> >


_________________________________________________________________
Share your memories online with anyone you want.
http://www.microsoft.com/singapore/windows/windowslive/products/photos-share.aspx?tab=1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090920/1237f1ec/attachment.html 


More information about the Qt-interest-old mailing list