Python Qt QListWidget Double Clicked

Cloak picture Cloak · Oct 13, 2012 · Viewed 9.8k times · Source

I want to add double clicked attribute for my QListWidget objects.

My command line does not work:

   self.connect(self.listWidget, QtCore.SIGNAL("itemDoubleClicked(QtGui.QListWidgetItem)"), self.showItem)

How to add double clicked attribute ? How to give object parameter to QtCore.SIGNAL.

Answer

ekhumoro picture ekhumoro · Oct 13, 2012

The reason why the signal connection did not work, is that you are using the wrong signature for QListWidget.itemDoubleClicked. It should instead look like this:

self.connect(self.listWidget,
             QtCore.SIGNAL("itemDoubleClicked(QListWidgetItem *)"),
             self.showItem)

However, I would advise that you avoid using this method of connecting signals altogther, and switch to the new-style syntax instead. This would allow you to rewrite the above code like this:

self.listWidget.itemDoubleClicked.connect(self.showItem)

Which is not only simpler and cleaner, but also much less error-prone (in fact, an exception will be raised if the wrong signal name/signature is used).