Sending custom PyQt signals?

Enfors picture Enfors · Apr 6, 2010 · Viewed 36k times · Source

I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.

  1. Main/GUI thread.

  2. Twitter fetch thread - fetches data from Twitter every X minutes.

So, every X minutes my Twitter thread downloads a new set of status updates (a Python list). I want to hand this list over to the Main/GUI thread, so that it can update the window with these statuses.

I'm assuming that I should be using the signal / slot system to transfer the "statuses" Python list from the Twitter thread, to the Main/GUI thread. So, my question is twofold:

  1. How do I send the statuses from the Twitter thread?

  2. How do I receive them in the Main/GUI thread?

As far as I can tell, PyQt can by default only send PyQt-objects via signals / slots. I think I'm supposed to somehow register a custom signal which I can then send, but the documentation on this that I've found is very unclear to a newbie like me. I have a PyQt book on order, but it won't arrive in another week, and I don't want to wait until then. :-)

I'm using PyQt 4.6-1 on Ubuntu

Update:

This is an excert from the code that doesn't work. First, I try to "connect" the signal ("newStatuses", a name I just made up) to the function self.update_tweet_list in the Main/GUI thread:

QtCore.QObject.connect(self.twit_in,
                       QtCore.SIGNAL("newStatuses (statuses)"),
                       self.update_tweet_list)

Then, in the Twitter thread, I do this:

self.emit(SIGNAL("newStatuses (statuses)"), statuses)

When this line is called, I get the following message:

QObject::connect: Cannot queue arguments of type 'statuses'
(Make sure 'statuses' is registered using qRegisterMetaType().)

I did a search for qRegisterMetaType() but I didn't find anything relating to Python that I could understand.

Answer

Mark Visser picture Mark Visser · Apr 7, 2010

You can also do this, which is much more pythonic (and readable!).

# create a signal equivalent to "void someSignal(int, QWidget)"
someSignal = QtCore.pyqtSignal(int, QtGui.QWidget)

# define a slot with the same signature
@QtCore.pyqtSlot(int, QtGui.QWidget)
def someSlot(status, source):
    pass

# connect the signal to the slot
self.someSignal.connect(self.someSlot)