Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to connect a signal from the controller in PyQt4? (iOS like MVC structure in PyQt4)
    primarykey
    data
    text
    <p>Why doesn't the following example work?</p> <pre><code>from PyQt4 import QtGui import sys class TestView(QtGui.QWidget): def __init__(self): super(TestView, self).__init__() self.initUI() def initUI(self): self.btn = QtGui.QPushButton('Button', self) self.btn.resize(self.btn.sizeHint()) self.btn.move(50, 50) class TestViewController(): def __init__(self, view): view.btn.clicked.connect(self.buttonClicked) view.show() def buttonClicked(self): print 'clicked' def main(): app = QtGui.QApplication(sys.argv) view = TestView() TestViewController(view) app.exec_() if __name__ == '__main__': main() </code></pre> <p>The example is supposed to represent an MVC structure (like the one in <a href="http://www.oracle.com/technetwork/articles/javase/index-142890.html" rel="nofollow">Figure 4</a> -- without the Model) where the controller (<code>TestViewController</code>) receives a reference to the view (<code>TestView</code>) and connects the clicked signal from the view's button <code>view.btn</code> to its function <code>self.buttonClicked</code>.</p> <p>I'm sure the line <code>view.btn.clicked.connect(self.buttonClicked)</code> is executed but, apparently, it has no effect. Does anyone knows how to solve that?</p> <hr> <p><strong>Update (awful solution):</strong></p> <p>In the example, if I replace the line</p> <pre><code>view.btn.clicked.connect(self.buttonClicked) </code></pre> <p>with</p> <pre><code>view.clicked = self.clicked view.btn.clicked.connect(view.clicked) </code></pre> <p>it works. I'm still not happy with that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload