[PyQt] qwidget launching problem

Phil Thompson phil at riverbankcomputing.com
Sun May 6 18:55:07 BST 2012


On Sun, 06 May 2012 20:25:15 +0300, Enes Albay <albayenes at gmail.com>
wrote:
> Hi,
> 
> 
> If i implement my class like below:
> 
> class MainWindow(QtGui.QMainWindow):
>      def __init__(self):
>          super(MainWindow, self).__init__()
>          self.initUI()
> 
>      def initUI():
>          ....
>          ....
>          self.showButton = QtGui.QPushButton(u"KlavyeyiGoster", 
> self.mainWidget)
>          self.showButton.resize(self.showButton.sizeHint())
>          self.showButton.clicked.connect(self.widgetClicked)
>          ....
>          ....
> 
> 
>      def widgetClicked(self):
>          self.newWidget = QtGui.QWidget();
>          self.newWidget.setGeometry(0, 0, 300, 300)
>          self.newWidget.show()
> 
> when i click Button, newWidget is constructed and showed. But if i 
> change "widgetClicked" method like this:
> 
> def widgetClicked(self):
>          newWidget = QtGui.QWidget();
>          newWidget.setGeometry(0, 0, 300, 300)
>          newWidget.show()
> 
> when i clicked the button, it doesn't show anything. There are no 
> compilation or run time errors.
> 
> What is the reason of this difference?

In the second version you aren't keeping a reference to the widget (either
explicitly or implicitly by giving it a parent) so it is getting garbage
collected when widgetClicked() returns.

Phil


More information about the PyQt mailing list