[PyQt] qwidget launching problem

Detlev Offenbach detlev at die-offenbachs.de
Sun May 6 18:55:25 BST 2012


Am Sonntag, 6. Mai 2012, 20:25:15 schrieb Enes Albay:
> 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?

The difference is called Garbage Collection. In the later case the newly 
created object is garbage collected as sson as the method returns. You have to 
keep a local reference as long as the widget is needed.

Regards,
Detlev
-- 
Detlev Offenbach
detlev at die-offenbachs.de


More information about the PyQt mailing list