Qt-interest Archive, May 2008
QThread and Signals
Message 1 in thread
How do I send signals between 2 threads?
First I had just the main-thread and another thread and the slot has not
been called since I always call QApplication::processEvents().
But now I have an additional thread that must emit a signal to the other
thread (not main), and the slot isn't called again.
I read a lot confusing things about using the QEventLoop in different
boards and the QT-documentation is not very detailed in this case.
It would be great to have a little example to see how this is working
exactly.
--
[ signature omitted ]
Message 2 in thread
Hi,
On Friday 30 May 2008 14:30:35 sTormtrOOpa wrote:
> How do I send signals between 2 threads?
> First I had just the main-thread and another thread and the slot has not
> been called since I always call QApplication::processEvents().
>
> But now I have an additional thread that must emit a signal to the other
> thread (not main), and the slot isn't called again.
you need to use queued connections and make sure that your thread which
contains your slot to be called has a running event loop. It is not necessary
to have a running event loop in the thread that emits the signal (although
you can if you like).
HTH,
Sean
--
[ signature omitted ]
Message 3 in thread
I tried this.
First I called QThread::exec() after calling start(). But the program
stops the execution at this point.
Second I created a QEventLoop-Object as a member of my QThread-class.
When I call the exec() for this object the result is the same.
--
[ signature omitted ]
Message 4 in thread
You need to call QThread::exec() inside the QThread::run()
implementation of your QThread subclass.
Make sure the object's slot which you will sent the signal to is
living in your QThread subclass (created in the run() implementation).
Regards,
Jesse
On Fri, May 30, 2008 at 9:13 PM, sTormtrOOpa <sTormtrOOpa@xxxxxxx> wrote:
> I tried this.
> First I called QThread::exec() after calling start(). But the program stops
> the execution at this point.
>
> Second I created a QEventLoop-Object as a member of my QThread-class.
> When I call the exec() for this object the result is the same.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 5 in thread
Thx for the suggestion but:
void myThread::run(void)
{
QThread::exec(); //execution stops here!
forever
{
doSomething;
}
}
--
[ signature omitted ]
Message 6 in thread
Hi,
On Friday 30 May 2008 16:09:05 sTormtrOOpa wrote:
> Thx for the suggestion but:
>
> void myThread::run(void)
> {
>
> QThread::exec(); //execution stops here!
Execution will block here until that event loop ends.
I think what you are after is something like this...
class MyFirstThread : public QThread
{
Q_OBJECT
public:
MyFirstThread( QObject* parent = 0 )
: QThread( parent ),
m_receiver( 0 )
{}
virtual ~MyFirstThread()
{
if ( m_receiver )
delete m_receiver;
}
virtual void run()
{
m_receiver = new MyReceiverObject;
connect( thread2, SIGNAL( mySignal() ), m_receiver, SLOT(
mySlot() ) );
exec();
}
private:
MyReceiverObject* m_reciever;
};
class MySecondThread : public QThread
{
Q_OBJECT
public:
MySecondThread( QObject* parent = 0 ) {}
virtual ~MySecondThread() {}
virtual void run()
{
forever {
// Do some stuff
emit mySignal();
}
}
signals:
void mySignal();
};
class MyReceiverObject : public QObject
{
Q_OBJECT
public:
MyReceiverObject() {}
~MyReceiverObject() {}
public slots:
void mySlot()
{
// Do some stuff in response to mySignal()
}
};
Notice how the receiving thread has a running event loop.
HTH,
Sean
--
[ signature omitted ]
Message 7 in thread
In my case I want to process different things in the run-method of my
receiving thread AND to receive signals. The run-method of the
"MyFirstThread" blocks at the end. I couldn't live with that. The
difference to my program is that the slot is not capsuled in an extra
object which is created in the run method.
Does that mean that I always have to capsule my exec() in a seperate
thread? This would not be qt-like-easy!
I just want to have two threads (next to the main thread) which can
simply call each others slots easily.
--
[ signature omitted ]
Message 8 in thread
Hi,
sTormtrOOpa wrote:
> In my case I want to process different things in the run-method of my
> receiving thread AND to receive signals. The run-method of the
> "MyFirstThread" blocks at the end. I couldn't live with that. The
> difference to my program is that the slot is not capsuled in an extra
> object which is created in the run method.
> Does that mean that I always have to capsule my exec() in a seperate
> thread? This would not be qt-like-easy!
Why not? This is exactly what you do in the main thread. In your main()
you normally create a QMainWindow or QDialog or similar subclass and
then call exec(). Just create an object of a class within QThread::run()
that does your work instead of having it directly in run(). That way
your thread can do what you need and still respond to signals from that
or other threads. This really shouldn't be a great deal of work to do.
> I just want to have two threads (next to the main thread) which can
> simply call each others slots easily.
Then you need to call exec() from the thread that is to receive signals
and act upon them. This makes sense if you think about it. If it did not
have its own event loop you would be asking that thread of execution to
be executing your run() function and another slot function simultaneously!
I hope this makes sense.
Cheers,
Sean
--
[ signature omitted ]