I am following some tutorials and trying to customize the list model. In my main window, there are two kinds of list that access the same model. When I update an item in one list, the other list is not updated until it receives focus (I click on it). Thus, it seems that the dataChanged signal is not emitted, but I cannot understand how my code is different from any of the examples that I am based on.
main.py
class Main(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.ui = uic.loadUi("mainwindow.ui", self)
data = [10,20,30,40,50]
myModel = model.MyListModel(data)
self.ui.listView.setModel(myModel)
self.ui.listView_2.setModel(myModel)
model.py
class MyListModel(QtCore.QAbstractListModel):
def __init__(self, data=[], parent=None):
super(MyListModel, self).__init__(parent)
self.__data = data
def rowCount(self, parent=QtCore.QModelIndex()):
return len(self.__data)
def data(self, index, role=QtCore.Qt.DisplayRole):
row = index.row()
if role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole):
return str(self.__data[row])
if role == QtCore.Qt.ToolTipRole:
return 'Item at {0}'.format(row)
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role=QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
self.__data[index.row()] = value
self.dataChanged.emit(index, index)
return True
return False
Can anyone see what is wrong here? FYI I am using PyQT5.2.1 and Python 3.3.
source
share