Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect double-click event of QListView with method in PyQt4

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() and entered() methods too, but they do not work either. These events are all listed in the documentation here.

like image 453
Paul Avatar asked Dec 22 '10 17:12

Paul


2 Answers

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.

like image 89
Paul Avatar answered Sep 17 '22 14:09

Paul


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.

like image 29
Moayyad Yaghi Avatar answered Sep 19 '22 14:09

Moayyad Yaghi