Search code examples
pythonpython-3.xpyqtqlistviewpyqt5

PyQt QListView retrieving item after item edited by user interface


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?


Solution

  • What i would rather do when i work with Model View programming. is make the Model rather than using the default ones.

    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)
    

    This is an example of customized Model. The setData function updates the value.