PDA

View Full Version : how do you reload a listView?



implor
16th May 2010, 21:24
how do you reload a listView? When I add a object to LIBARY I want to reload the list so all items is showed. how do I do that?


here is my code


#------------------------------------------------------------
#------------------------------------------------------------

# Hantera biblotek
class manageLibary(QtGui.QDialog):
def __init__(self, parent):
QtGui.QDialog.__init__(self, parent)
self.ui = ManageLibary()
self.ui.setupUi(self)
self.model = manageLibaryModel(self)
self.ui.listView.setModel(self.model)
self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.validateInput)


def validateInput(self):
if self.ui.lineEdit.text() != "":
# make validator later here for
LIBARY.append(bibliotek(self.ui.lineEdit.text()))
print self.ui.lineEdit.text()
print len(LIBARY)
self.emit(QtCore.SIGNAL("dataChanged()"))



#------------------------------------------------------------
# Manage Libary Model
#------------------------------------------------------------
#
class manageLibaryModel(QtCore.QAbstractListModel):
def __init__(self, parent):
QtCore.QAbstractListModel.__init__(self, parent)


def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return LIBARY[index.row()].name

elif role == QtCore.Qt.UserRole:
return LIBARY[index.row()]

def rowCount(self, parent=QtCore.QModelIndex()):
return len(LIBARY)

wysota
16th May 2010, 21:59
Your model should emit proper signals when there is something added to it. You shouldn't access the underlying datastructure directly unless the model can detect that.

implor
16th May 2010, 22:40
I got it to work. But i did not use signals. how would you do in my case?

my new code:

#------------------------------------------------------------
#------------------------------------------------------------

# Hantera biblotek
class manageLibary(QtGui.QDialog):
def __init__(self, parent):
QtGui.QDialog.__init__(self, parent)
self.ui = ManageLibary()
self.ui.setupUi(self)
self.model = manageLibaryModel(self)
self.ui.listView.setModel(self.model)
self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.validateInput)
#for i in dir(self.ui.listView):
# print i

def validateInput(self):
if self.ui.lineEdit.text() != "":
# make validator later here for
data = bibliotek(self.ui.lineEdit.text())
print self.ui.lineEdit.text()
print len(LIBARY)
self.model.addItem(data)




#------------------------------------------------------------
# Manage Libary Model
#------------------------------------------------------------
#
class manageLibaryModel(QtCore.QAbstractListModel):
def __init__(self, parent):
QtCore.QAbstractListModel.__init__(self, parent)


def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
return LIBARY[index.row()].name

elif role == QtCore.Qt.UserRole:
return LIBARY[index.row()]

def rowCount(self, parent=QtCore.QModelIndex()):
return len(LIBARY)

def addItem(self, item):
self.beginInsertRows(QtCore.QModelIndex(), len(LIBARY), len(LIBARY))
LIBARY.append(item)
self.endInsertRows()

wysota
16th May 2010, 22:41
You are using signals. beginInsertRows() and endInsertRows() emit proper signals.

implor
16th May 2010, 23:04
oh, I see :). btw can you manually reload a listView or do you have to use the framework ?

squidge
16th May 2010, 23:21
If you add items to the model, they will appear in the views almost instantly.

If the model changes the data substantially, you can use the layoutAboutToBeChanged and layoutChanged functions to emit appropriate signals.