[PyQt] Question about QListView

Phil Thompson phil at riverbankcomputing.com
Thu Dec 18 12:29:44 GMT 2008


On Thu, 18 Dec 2008 13:11:19 +0100, Ralph Kube <ralphkube at googlemail.com>
wrote:
> Hey people,
> I have a simple question about using QListView in my application.
> Lets say, I want QListView to display a number of strings.
> On the command line the following works:
> 
> str_list = ['str1', 'str2', ... , 'strn']
> qstr = QStringList(str_list)
> qstr_model = QStringListModel(qstr)
> lv = QListView()
> lv.setModel(qstr_model)
> lv.show()
> 
> I get a ListView with the strings in there. Great.
> Now I got the following code in my app and it doesn't show anything:
> 
> 
> class ListViewTest(QtGui.QMainWindow):
> 	def __init__(self, *args):
> 		apply(QtGui.QMainWindow.__init__, (self, ) + args)
> 
> 		self.setGeometry(300, 300, 300, 50)
> 		self.setWindowTitle('ListViewTest')
> 
> 		qstr = QtCore.QStringList(QtCore.QString('foo'))
> 		qstr_model = QtGui.QStringListModel(qstr)
> 		self.lv = QtGui.QListView(self)
> 		self.lv.move(120,10)
> 		self.lv.setModel(qstr_model)
> 
> 		self.pb = QtGui.QPushButton('Update', self)
> 		self.pb.move(10,10)
> 
> 		QtCore.QObject.connect(self.pb, QtCore.SIGNAL('clicked()'), 
> self.update_list)
> 
> 
> 	def update_list(self):
> 
> 		qstr = QtCore.QStringList(QtCore.QString('bar'))
> 		qstr_model = QtGui.QStringListModel(qstr)
> 		self.lv.setModel(qstr_model)
> 		print 'Button pressed'
> 
> 
> app = QtGui.QApplication(sys.argv)
> mywin = ListViewTest()
> mywin.show()
> app.exec_()
> 
> 
> Any ideas about that? I surely am confused.

Keep an explicit reference to your model. It is being garbage collected.

Phil


More information about the PyQt mailing list