I have a QListView with a few items which are loaded from a Python list. I want to modify that list's elements from the QListView by double click and rewriting the item. Example:
I have this list self.item_list = ["A", "B", "BBB", "AA", "ABC", "AA", "ABG", "CCB", "CCC", "ABB"]
and I want to modify 'ABC'
The procedure it should be:
- Double click
- Rewrite item
- Enter
My code is:
LView.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LView(object):
def setupUi(self, LView):
LView.setObjectName("LView")
LView.resize(356, 294)
self.centralwidget = QtWidgets.QWidget(LView)
self.centralwidget.setObjectName("centralwidget")
self.listView = QtWidgets.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(45, 34, 256, 192))
self.listView.setObjectName("listView")
LView.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(LView)
self.menubar.setGeometry(QtCore.QRect(0, 0, 356, 21))
self.menubar.setObjectName("menubar")
LView.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(LView)
self.statusbar.setObjectName("statusbar")
LView.setStatusBar(self.statusbar)
self.retranslateUi(LView)
QtCore.QMetaObject.connectSlotsByName(LView)
def retranslateUi(self, LView):
_translate = QtCore.QCoreApplication.translate
LView.setWindowTitle(_translate("LView", "List View"))
main.py
from LView import Ui_LView
from PyQt5 import QtCore, QtGui, QtWidgets
class ListView(QtWidgets.QMainWindow, Ui_LView):
def __init__(self):
super().__init__()
self.setupUi(self)
self.listView.installEventFilter(self)
self.model = QtGui.QStandardItemModel()
self.item_list = ["A", "B", "BBB", "AA", "ABC", "AA", "ABG", "CCB", "CCC", "ABB"]
for i in self.item_list:
self.model.appendRow(QtGui.QStandardItem(i))
self.listView.setModel(self.model)
def eventFilter(self, obj, event):
print("event: ", event)
return super().eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = ListView()
ui.show()
sys.exit(app.exec_())
As far I printing the events in eventFilter
but many of them are triggering so I don't exactly what event I need to manage. The program it's printing events like:
PyQt5.QtGui.QPaintEvent
PyQt5.QtGui.QFocusEvent
PyQt5.QtCore.QEvent
PyQt5.QtCore.QTimerEvent
PyQt5.QtGui.QInputMethodQueryEvent
PyQt5.QtGui.QHelpEvent
PyQt5.QtGui.QKeyEvent
I suspect the events which can be useful are QInputMethodQueryEvent
, QEvent
or QKeyEvent
and I tried to catch them with if type(event) == type(QtGui.QInputMethodQueryEvent())
for analyse them but got errors like QInputMethodQueryEvent(QInputMethodQueryEvent): not enough arguments
. I need to pass them parameters that I unknown. Can someone give me and advice about this? Any suggestion it would be greatly appreciated