0

I'm working with Python 3.3 PyQt5. I have an QListView widget which is contains some items. I want to retrieve item when user double-click to item and edit it so I can save its new value to a database. But I can't find any SIGNAL like edit finished or something useful.

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()

I displayed list within this way. I want to retrieve edited word with editWord() method. How can I execute editWord() after item in the listView changed?

4

1 回答 1

0

当我使用模型视图编程时,我宁愿做什么。是制作模型而不是使用默认模型。

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 函数更新值。

于 2014-01-21T02:52:38.473 回答