QThread Data Access Method
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
Re: QThread Data Access Method
call a public fuction in the thread class
ex:
Code:
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()
Re: QThread Data Access Method
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.
Re: QThread Data Access Method
Quote:
Originally Posted by
schan117
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.
Re: QThread Data Access Method
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
Re: QThread Data Access Method
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.
Re: QThread Data Access Method
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
Re: QThread Data Access Method
follow this example ... it suits exact requirement for u on QThread
http://doc.trolltech.com/4.0/network...uneclient.html