我正在使用Python 3.3 PyQt5。我有一个QListView小部件,其中包含一些项目。我想在用户双击项目并编辑它时检索项目,以便将其新值保存到数据库中。但我找不到任何SIGNAL,如编辑完成或有用的东西。
class MainWin(QtWidgets.QMainWindow):
itemName = ""
def __init__(self, inheritance=None):
super(MainWin, self).__init__()
self.ui=uic.loadUi("MainWin.ui", self)
self.wordList = FileProc.WordStorage().readWordFile()
self.showListView()
self.itemName = ""
def showListView(self, file = 'wordlist.db'):
MainWin.wordList = FileProc.WordStorage().readWordFile(file)
model = QtGui.QStandardItemModel(self.listView)
for row in MainWin.wordList:
item = QtGui.QStandardItem(row)
model.appendRow(item)
self.listView.setModel(model)
def editWord(self):
itemIndex = self.listView.selectedIndexes()
MainWin.itemName = itemIndex[0].data()
我以这种方式显示了列表。我想用editWord()方法检索已编辑的单词。如何在listView中的项目更改后执行editWord()?
答案 0 :(得分:0)
当我使用Model View编程时,我宁愿做什么。是模型而不是默认模型。
class ListMOdel(QtCore.QAbstractListModel):
def __init__(self , data=[] ,parent=None):
QtCore.QAbstractListModel.__init__(self,parent)
self.__data=data
def rowCount(self ,parent):
return len(self.__data)
def data(self,index,role):
if role == QtCore.Qt.DisplayRole:
row=index.row()
value = self.__data[row]
return value
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:
row= index.row()
self.__data[row]=value
return True
return False
class MainWin(QtWidgets.QMainWindow):
itemName = ""
def __init__(self, inheritance=None):
super(MainWin, self).__init__()
self.ui=uic.loadUi("MainWin.ui", self)
self.wordList = FileProc.WordStorage().readWordFile()
self.showListView()
self.itemName = ""
def showListView(self, file = 'wordlist.db'):
MainWin.wordList = FileProc.WordStorage().readWordFile(file)
data=[]
for row in MainWin.wordList:
data.append(row)
model = ListMOdel(data)
self.listView.setModel(model)
这是自定义模型的示例。 setData函数更新该值。