Connect the QListView double-click event to a method in PyQt4

I have a PyQt QListView object, and I want the method to run on double-click. 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!"

Ive tried the "clicked ()" and "entered ()" methods, but they also don't work. All these events are listed in the documentation: http://bit.ly/govx1h

+3
source share
3 answers

This seems to work if:

self.connect(lb, SIGNAL('doubleClicked()'), self.someMethod)

Replaced with the new syntax:

lb.doubleClicked.connect(self.someMethod)

The latter is much more elegant. I still don't know why the original syntax did not work.

+11
source

,

self.connect(lb,QtCore.SIGNAL("itemDoubleClicked (QListWidgetItem *)"),self.someMethod)

pyqt, .

, . , :)

+3

"itemDoubleClicked" is the signal emitted by QListWidget, not QListView. I tested the Moayyad Yaghi proposal and it didn't work for me at least on Qt 4 with python 2.5

Although, lb.doubleClicked.connect (self.someMethod) works fine.

+1
source

Source: https://habr.com/ru/post/1781699/


All Articles