PyQt signal between QObjects

Posted by geho on Stack Overflow See other posts from Stack Overflow or by geho
Published on 2012-09-05T07:01:09Z Indexed on 2012/09/05 15:38 UTC
Read the original article Hit count: 199

Filed under:
|
|

I'm trying to make a view and controller in PyQt where the view is emitting a custom signal when a button is clicked, and the controller has one of its methods connected to the emitted signal. It does not work, however. The respond method is not called when I click the button. Any idea what I did wrong ?

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import QPushButton, QVBoxLayout, QDialog, QApplication 

class TestView(QDialog):
    def __init__(self, parent=None):
        super(TestView, self).__init__(parent)
        self.button = QPushButton('Click')
        layout = QVBoxLayout()
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.connect(self.button, SIGNAL('clicked()'), self.buttonClicked)

    def buttonClicked(self):
        self.emit(SIGNAL('request'))

class TestController(QObject):
    def __init__(self, view):
        self.view = view
        self.connect(self.view, SIGNAL('request'), self.respond)

    def respond(self):
        print 'respond'

app = QApplication(sys.argv)
dialog = TestView()
controller = TestController(dialog)
dialog.show()
app.exec_()

© Stack Overflow or respective owner

Related posts about pyqt

Related posts about signals