Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, February 2007
Sending a signal from a non QObject class


Message 1 in thread

Hello qt-jambi fellows,

After some research I couldn't find out how to send a signal from an event loop starting by an external library.
I'm using a library called smack as a jabber back end (don't worry, this is QT related). But when I tried to send a signal from the message listener (a thread started by this library) I get this error which wasn't thrown in the TP3 :

"QObject used from outside its own thread"

The doc says you have to derive your objects from QObject, but I can't since this is an external library. Well it's open source, but I believe there's another way than hacking the library's code. Does anyone has an idea ?

Thanks,
FranÃois Prunier


Message 2 in thread

FranÃois PRUNIER wrote:
> Hello qt-jambi fellows,
> 
> After some research I couldn't find out how to send a signal from an
> event loop starting by an external library.
> I'm using a library called smack as a jabber back end (don't worry,
> this is QT related). But when I tried to send a signal from the message
> listener (a thread started by this library) I get this error which
> wasn't thrown in the TP3 :
> 
> "QObject used from outside its own thread"

This is correct. Since 1.0.0-beta we check the thread affinity of the 
object before allowing the user to call the method. This is to prevent 
miss-use which often leads to crashes, which there were quite a few of 
on the list so far.

The idea of threading in Qt is that QObject belongs to a thread and 
cannot be touched from outside it. The only way to communicate with that 
objecct is to post events to it or do a queued connection, both of which 
required the thread to run an event loop at some point. See more at:

http://doc.trolltech.com/qtjambi-1.0/com/trolltech/qt/threads.html

> The doc says you have to derive your objects from QObject, but I
> can't
> since this is an external library. Well it's open source, but I believe
> there's another way than hacking the library's code. Does anyone has an
> idea ?

Where in the docs does it say this? This needs to be updated. Since 
1.0.0-beta there is a class com.trolltech.qt.QSignalEmitter that you can 
derive which doesn't have the threading rules of QObject.

-

If you're scenario is that you have a thread A which gets the message 
and you need to notify an object in thread B you can do either of the 
following things:

1. If the receiver is in the GUI thread you can use 
QApplication.invokeLater() and pass a runnable. The Runnable will be 
executed by the GUI eventloop when its idle.

2. You can set up a queued connection between an object in thread A and 
an object in thread B and emit the data from thread A. This requires you 
to run an event loop in thread B to process the data.

3. You can create your own custom event and post the data to the 
receiver in thread B. This requires you to run an event loop in thread B.

-

I hope this helps,
Gunnar