Connect double-click event of QListView with method in PyQt4
Solution 1
It seems to work if:
self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod)
Is replaced with the new syntax of:
lb.doubleClicked.connect(self.someMethod)
The latter is much more elegant too. I still do not know why the original syntax did not work, however.
Solution 2
It will also work if you use:
self.connect(lb,QtCore.SIGNAL("itemDoubleClicked (QListWidgetItem *)"),self.someMethod)
check the pyqt reference, then copy and paste the signal as is.
I know you already solved it. but I think knowing more than one method will be better.
Solution 3
itemDoubleClicked
is a signal emitted by QListWidget
and not QListView
. I tested Moayyad Yaghi's suggestion and it did not work for me at least on Qt 4 with python 2.5
Though, lb.doubleClicked.connect(self.someMethod)
works perfectly fine.
Momo
I work as a software development manager, working mostly with C#, Azure and SQL Server.
Updated on July 09, 2022Comments
-
Momo almost 2 years
I’ve got a PyQt
QListView
object, and I want a method to run when it is double-clicked. This should be trivial, but it doesn't seem to work. My code is as follows:class MainWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) lb = QListView() self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod) grid = QGridLayout() grid.addWidget(lb, 0, 0) centralWidget.setLayout(grid) def someMethod(self): print "It happened!"
I’ve tried
clicked()
andentered()
methods too, but they do not work either. These events are all listed in the documentation here. -
Momo over 13 yearsThat doesn’t seem to work for me in pyqt4. I also tried self.connect(lb, SIGNAL('itemSelectionChanged()'), self.someMethod), but that doesn’t work either. It’s annoying, as I now want to use a keypressed signal, which doesn't seem to be available via lb.keyPressed.
-
grego over 4 yearsLooks like it comes from QAbstractItemView: doc.qt.io/qt-5/qabstractitemview.html#doubleClicked