Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:19:20+00:00 2026-06-10T20:19:20+00:00

Why doesn’t the following example work? from PyQt4 import QtGui import sys class TestView(QtGui.QWidget):

  • 0

Why doesn’t the following example work?

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()

The example is supposed to represent an MVC structure (like the one in Figure 4 — without the Model) where the controller (TestViewController) receives a reference to the view (TestView) and connects the clicked signal from the view’s button view.btn to its function self.buttonClicked.

I’m sure the line view.btn.clicked.connect(self.buttonClicked) is executed but, apparently, it has no effect. Does anyone knows how to solve that?


Update (awful solution):

In the example, if I replace the line

view.btn.clicked.connect(self.buttonClicked)

with

view.clicked = self.clicked
view.btn.clicked.connect(view.clicked)

it works. I’m still not happy with that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:19:21+00:00Added an answer on June 10, 2026 at 8:19 pm

    The reason it is not working is because the controller class is being garbage collected before you can ever click anything for it.

    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.

    If you store your controller to a variable, it will protect it from collection.

    So if you change your code above to read:

    ctrl = TestViewController(view)
    

    You’ll be all set.

    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:

    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()
    

    Edit: Clarifying the MVC of Qt

    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 http://bitesofcode.blogspot.com/2011/10/introduction-to-designer.html but many can be found online.

    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:

    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()
    

    Edit 2: Storing UI references

    If it is easier to visualize this concept, you Can also store a reference to the generated UI object:

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This doesn't work: $to = 'myemail@gmail.com'; $from = 'test@test.com'; $subj = 'test'; $message =
Python doesn't support complicated anonymous functions. What's a good alternative? For example: class Calculation:
Why doesn't this work? HTML <div class=gallery> <div class=viewport> <div class=wrapper> <img src=img/boat1.png />
Why doesn't the following code compile? template <class T> void foo_bar(T =5 , T
Why doesn't the following style work? I have in other places the exact same
Doesn't work with other modules, but to give an example. I installed Text::CSV_XS with
Why doesn't the following work? It appears that the literal zero at the end
Doesn't seem to work for me, maybe I just doing it wrong
Doesn't object initialization outside of a constructor break encapsulation ? Given: class MyClass {
This doesn't work: int number = 1; String numberstring = IntToString(number); I get The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.