[PyQt] difference between inits

Phil Thompson phil at riverbankcomputing.com
Thu Nov 13 10:56:17 GMT 2008


On Wed, 12 Nov 2008 14:46:20 -0200, "Tobias Ramos" <tobalas at gmail.com>
wrote:
> Hi all..i have some doubts about something is happening in my code. here
is
> it:
> 
>    w = window()
>    f = QtCore.QFile(":/default.txt")
>    f.open(QtCore.QIODevice.ReadOnly)
>    model = TreeModel(QtCore.QString(f.readAll()))
>    f.close()
>    w.ui.lineEdit.setFocus()
>    w.ui.treeView.setModel(model)
>    w.show()
> 
> this works only in init code of application ex:
> 
> if __name__ == "__main__":
>    app = QtGui.QApplication(sys.argv)
>    w = window()
>    f = QtCore.QFile(":/default.txt")
>    f.open(QtCore.QIODevice.ReadOnly)
>    model = TreeModel(QtCore.QString(f.readAll()))
>    f.close()
>    w.ui.lineEdit.setFocus()
>    w.ui.treeView.setModel(model)
>    w.show()
>    sys.exit(app.exec_())
> 
> my question is.. why this not work:
> 
> class window(QtGui.QWidget,Ui_Form):
> 
>     def __init__(self,parent=None):
>         QtGui.QWidget.__init__(self,parent)
>         self.ui = Ui_Form()
>         self.ui.setupUi(self)
>         self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"),
> self.populaCombo)
>          f = QtCore.QFile(":/default.txt")
>          f.open(QtCore.QIODevice.ReadOnly)
>          model = TreeModel(QtCore.QString(f.readAll()))
>          f.close()
>         self.ui.lineEdit.setFocus()
>         self.ui.treeView.setModel(model)
> 
> when i put on __init__ of inherited class even the file is read..
> thanks in advance.

Keep a reference to your model to prevent it from being garbage collected.

Phil


More information about the PyQt mailing list