[Qt-interest] How to do it... in a QThread?
K. Frank
kfrank29.c at gmail.com
Fri Aug 5 00:49:25 CEST 2011
Hi Luiz!
On Thu, Aug 4, 2011 at 6:20 PM, Luiz Vitor Martinez Cardoso
<grabber at gmail.com> wrote:
> Dear,
> I'm struggling on a problem.
> Let's suppose we have a Qt GUI Application with just a simple QButton.
> When the application is started the main constructor perform some operations
> and set A,B,C and D variables values;
> When I press the QButton I want to start a parallel process (Thread) to do
> some operations using A, B, C and D. I will use a QMutex to keep everything
> safe.
> How can I make A, B, C and D visible to Thread?
In Qt, as in most threading systems in general, the threads in a
process all share the
same address space. So, you can simply pass a pointer to the data in question.
> What I mean is: How can I
> manipulate the content of A, B, C and D, perform something and update the
> values?
Let's say that you have allocated a structure containing A, B, C, and
D on the heap:
ABCDStruct *pABCD = new ABCDStruct;
If, for example, your design is such that you derive your own thread class from
QThread, you could pass pABCD to that class's constructor. The run method
of the QThread-derived class will now have access to A, B, C, and D through
its copy of pABCD struct.
As long as you use some sort of synchronization method -- e.g., the QMutex
you mentioned -- both your child thread and the main gui thread can read and
write A, B, C, and D, thus sharing the data and communicating with one another.
> Best regards,
> Luiz.
Good luck.
K. Frank
More information about the Qt-interest-old
mailing list