how do you reload a listView?
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
Code:
#------------------------------------------------------------
#------------------------------------------------------------
# Hantera biblotek
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
#------------------------------------------------------------
#
def __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()]
return len(LIBARY)
Re: how do you reload a listView?
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.
Re: how do you reload a listView?
I got it to work. But i did not use signals. how would you do in my case?
my new code:
Code:
#------------------------------------------------------------
#------------------------------------------------------------
# Hantera biblotek
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
#------------------------------------------------------------
#
def __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()]
return len(LIBARY)
def addItem(self, item):
self.
beginInsertRows(QtCore.
QModelIndex(), len
(LIBARY
), len
(LIBARY
)) LIBARY.append(item)
self.endInsertRows()
Re: how do you reload a listView?
You are using signals. beginInsertRows() and endInsertRows() emit proper signals.
Re: how do you reload a listView?
oh, I see :). btw can you manually reload a listView or do you have to use the framework ?
Re: how do you reload a listView?
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.