Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

python - Event for modify QListView items in PyQt5

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'

enter image description here

The procedure it should be:

  1. Double click
  2. Rewrite item
  3. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...