Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason it is not working is because the controller class is being garbage collected before you can ever click anything for it.</p> <p>When you set view.clicked = self.clicked, what you're actually doing is making one of the objects from the controller persist on the view object so it never gets cleaned up - which isn't really the solution.</p> <p>If you store your controller to a variable, it will protect it from collection.</p> <p>So if you change your code above to read:</p> <pre><code>ctrl = TestViewController(view) </code></pre> <p>You'll be all set.</p> <p>That being said - what exactly you are trying to do here, I am not sure...it seems you're trying to setup an MVC system for Qt - but Qt already has a pretty good system for that using the Qt Designer to separate the interface components into UI (view/template) files from controller logic (QWidget subclasses). Again, I don't know what you are trying to do and this may be a dumb down version of it, but I'd recommend making it all one class like so:</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) self.btn.clicked.connect(self.buttonClicked) def buttonClicked(self): print 'clicked' def main(): app = QtGui.QApplication(sys.argv) view = TestView() view.show() app.exec_() if __name__ == '__main__': main() </code></pre> <p><strong>Edit: Clarifying the MVC of Qt</strong></p> <p>So this above example doesn't actually load the ui dynamically and create a controller/view separation. Its a bit hard to show on here. Best to work through some Qt/Designer based examples/tutorials - I have one here <a href="http://bitesofcode.blogspot.com/2011/10/introduction-to-designer.html" rel="nofollow">http://bitesofcode.blogspot.com/2011/10/introduction-to-designer.html</a> but many can be found online.</p> <p>The short answer is, your loadUi method can be replace with a PyQt4.uic dynamic load (and there are a number of different ways to set that up) such that your code ultimately reads something like this:</p> <pre><code>from PyQt4 import QtGui import PyQt4.uic import sys class TestController(QtGui.QWidget): def __init__(self): super(TestController, self).__init__() # load view uifile = '/path/to/some/widget.ui' PyQt4.uic.loadUi(uifile, self) # create connections (assuming there is a widget called 'btn' that is loaded) self.btn.clicked.connect(self.buttonClicked) def buttonClicked(self): print 'clicked' def main(): app = QtGui.QApplication(sys.argv) view = TestController() view.show() app.exec_() if __name__ == '__main__': main() </code></pre> <p><strong>Edit 2: Storing UI references</strong></p> <p>If it is easier to visualize this concept, you Can also store a reference to the generated UI object:</p> <pre><code>from PyQt4 import QtGui import PyQt4.uic import sys class TestController(QtGui.QWidget): def __init__(self): super(TestController, self).__init__() # load a view from an external template uifile = '/path/to/some/widget.ui' self.ui = PyQt4.uic.loadUi(uifile, self) # create connections (assuming there is a widget called 'btn' that is loaded) self.ui.btn.clicked.connect(self.buttonClicked) def buttonClicked(self): print 'clicked' def main(): app = QtGui.QApplication(sys.argv) view = TestController() view.show() app.exec_() if __name__ == '__main__': main() </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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