[Qt-interest] Trying to understand Signal/slots.

Francisco Gonzalez Morell gzmorell at gmail.com
Mon Aug 23 10:09:12 CEST 2010


On Lunes 23 Agosto 2010 08:34:27 Thiago Macieira escribió:
> On Monday 23. August 2010 02.08.55 Francisco Gonzalez Morell wrote:
> > Is any problem with the slot stopProcess?
> 
> We don't know. You didn't post its body.

void RenderThread::stopProcess()
 {
     mutex.lock();
     m_abort = true;
     mutex.unlock();
 }

> 
> However, the fact that it's a slot in a class deriving from QThread does
> bring this blog to mind:
> 
> http://labs.trolltech.com/blogs/2010/06/17/youre-doing-it-wrong/
> 
> A slot controlling the thread is not wrong per se. A slot for doing work in
> the thread would be. But it really depends on what you put in that slot.
> 
> Before we go further, answer this question: in which thread is that slot
> run? You can only put slots in a class derived from QThread if you can
> answer that question.
I suppose the slot is run from the thread QThread derived was created.
That's why you use mutex, to avoid corruption. So it is supposed to be ok.

>From your comments I undersatand that you can not call the slot from the
implemented QThread::run()  method. 



>From what I have understand from the labs blog:
- It is not necessary anymore to derive from QThread, and reimplement 
QThread::run(), in order to use threads.
- The new (from Qt 4.3?) way is to derive the classess you need from QObject, 
include the SIGNALS and SLOTS you need, Create the objects you need from this 
classes and CONNECT the signals and slots from classes as needed. Create as 
QThreads as QObject derived objects you want to live in other threads.
Use moveToThread() method from QObject to attach each object to one thread.
Call to QThread::start() method in order to start each event loop.

That's ok.

But what if you want to share some data. May create a signal like this 
signal(QByteArray *), and then use the (QByteArray *) in another object 
leaving in another thread for writing or reading without concerns about 
corruption of data. (no need for mutex). I think it is not.
So the way should be i.e. create two signal (QByteArray) in each object, for 
sending a copy, then read or modify, and then if modified return the QByteArray 
with another signal (QByteArray)?.
And then what about imlicit sharing? Will QByteArray detach if trying to write 
in any of the threads?




More information about the Qt-interest-old mailing list