1

In Python Qt, I'm connecting a QListWidget signal to a slot, like this:

QtCore.QObject.connect(self.myList, QtCore.SIGNAL("itemClicked(QListWidgetItem *)"), self.ListEventHandler)

My question is: what does the trailing asterisk in QListWidgetItem * do?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
PProteus
  • 549
  • 1
  • 10
  • 23

2 Answers2

5

A couple of bullet points to explain (I'll try to avoid C++ syntax):

  • PyQt is a Python wrapper around Qt, which is written in C++.
  • Qt provides introspection for classes inheriting from QObject, frequently using strings to identify things. Python has native introspection, but C++ does not.
  • The syntax that you used is called "Old-Style Signals and Slots", which uses the C++ function signatures.
  • C++ has more types of variables than Python. In C++, a variable can be a value, reference, or pointer. Pointers have an asterisk after their type name.
  • QtCore.SIGNAL("itemClicked(QListWidgetItem *)") refers to a Qt signal called itemClicked that has a parameter that is a pointer to a QListWidgetItem, not the item itself.

In C++, this looks like:

 void itemClicked(QListWidgetItem *item);

Going back to strings for introspection, to identify a signal or slot, you drop the void, the ;, and the variable name (item), leaving:

 itemClicked(QListWidgetItem *)

Wrap the above in QtCore.SIGNAL() and a pair of quotes and you have:

QtCore.SIGNAL("itemClicked(QListWidgetItem *)")

What is a pointer?
There are a number of SO questions about this. Here is one with a number of analogies in the answers to simplify things for you.

If this is old-style syntax, what's new style?
Thanks to Frodon for bringing this up. PyQt has a more "pythonic" method of connecting signals to slots, in the format:

object.signalName.connect(otherObject.slotName)

In this case:

self.myList.itemClicked.connect(self.ListEventHandler)

Read more in the docs.

Community
  • 1
  • 1
jonspaceharper
  • 4,207
  • 2
  • 22
  • 42
  • Thanks, Jon! That's a very clear explanation. You've unravelled a few mysteries for me. – PProteus Jul 19 '16 at 12:05
  • 2
    @PProteus For the curiosoty, here is the same line of code, but in the new-style syntax: `self.myList.itemClicked.connect(self.ListEventHandler)` – Frodon Jul 19 '16 at 12:22
  • 2
    The old-style syntax is obsolete, and is no longer supported in pyqt5. You should not use it at all in new pyqt code - it's only supported in pyqt4 for backwards compatibility. – ekhumoro Jul 19 '16 at 15:32
2

It's C++ syntax for indicating that the function itemClicked is passed a pointer to a QListWidgetItem as its only argument.

You can think of this as being "pass by reference", rather than "pass by value".

James Elderfield
  • 2,389
  • 1
  • 34
  • 39
  • Thanks for your answer, James, but I'm using Python, not C++. – PProteus Jul 19 '16 at 11:22
  • 2
    Sure, but the signatures for PyQt signals are defined in C++ syntax unless I am completely mistaken. [This](http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html) certainly suggests that to be the case. – James Elderfield Jul 19 '16 at 11:28