PDA

View Full Version : QThread Data Access Method



schan117
14th August 2009, 03:38
Hi all:

Just got a basic question concerning QThread. Say if my QThread Object is inside another inherited QMainwindow which also contains other non-Qt classes. If i start a the thread, what should i do to make the run() function of QThread to have access to the members in the calling inherited QMainwindow class? Thanks in advance!

schan117

wagmare
14th August 2009, 05:27
call a public fuction in the thread class
ex:

public:
void startThread();
void run();

in void startThread()
{
if(!isRunning()){
start();
}
}

so in the mainwindow class call
thread->startThread();

and it will automatically call the run() of the thread()

caduel
14th August 2009, 07:38
pass the thread's constructor a pointer to the data you want to access.
Make sure you guard (i.e. lock) against read/write conflicts when accessing the same data in different threads. Also note that QWidgets may only be accessed from the gui thread.

wysota
14th August 2009, 07:41
If i start a the thread, what should i do to make the run() function of QThread to have access to the members in the calling inherited QMainwindow class? Thanks in advance!


You shouldn't do that because widget classes are not thread safe.

schan117
14th August 2009, 09:06
Hi all:

Thanks for the answers guys. So this means I can only pass to the thread a QWidget class but not some other classes? I am asking this because I want to QThread to access to the camera object of parent class to do capturing and display it on QLabel. Another way I think of is to use QTimer. Whats your ideas guys? Thanks!

schan117

wysota
14th August 2009, 09:24
No, QWidget is not thread-safe as well. You musn't call any of QWidget (or descendant) methods from within a worker thread. If you want to transfer data between the worker thread and the main thread, you have to use either events or signals.

schan117
17th August 2009, 16:38
So you mean what i should do is to setup the thread to emit a signal when inside run() of the QThread Class and connect it back to a slot of the parent class and from that access the data?

schan117

wagmare
18th August 2009, 06:25
follow this example ... it suits exact requirement for u on QThread
http://doc.trolltech.com/4.0/network-blockingfortuneclient.html