用于 openPersitentEditor 的 PyQt5 TypeError
Posted
技术标签:
【中文标题】用于 openPersitentEditor 的 PyQt5 TypeError【英文标题】:PyQt5 TypeError for openPersitentEditor 【发布时间】:2021-01-28 14:29:51 【问题描述】:大家好,我正在用 PYQT5 做一些考试准备,我从我的工作簿中的一个练习中创建了一个应用程序,我们必须让它显示一个课程列表,当你点击它们时,它会打开一个带有课程名称的消息框还有一个按钮,用户可以将课程添加到列表中。添加按钮应该在 listWidget 的最后一项上打开 QlineEdit,因此用户可以编辑该字段,但是我不断收到 TypeError 消息:
第 67 行,在 onAddButton 中 self.mylistWidget.openPersistentEditor(self, modelItem) TypeError: openPersistentEditor(self, QListWidgetItem): argument 1 has unexpected type 'UNISACourses'
import sys
from PyQt5.QtWidgets import (QListWidget, QLineEdit, QWidget, QMessageBox, QHBoxLayout, QAbstractItemView,
QApplication, QVBoxLayout, QPushButton, QButtonGroup)
from PyQt5.QtCore import pyqtSlot
from PyQt5 import Qt, QtGui
class MyListWidget(QListWidget, QLineEdit, QWidget):
"""
Subclassed QListWidget to allow for the closing of open editors and other modifications
"""
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setSelectionMode(QAbstractItemView.ExtendedSelection)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return:
print("Closing any persistent editor")
self.closePersistentEditor(self.model().index(self.count() - 1))
else:
super().keyPressEvent(event)
class UNISACourses(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Main Window Settings
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('Courses')
# Layout
self.main_layout = QVBoxLayout(self)
self.setLayout(self.main_layout)
# Main Widget
self.mylistWidget = MyListWidget()
self.mylistWidget.addItems(["COS1511", "COS1521", "COS1512", "MNB1512", "INF1505", "FAC1502"])
self.main_layout.addWidget(self.mylistWidget)
# Define a layout for the other buttons to exist in for flexibility with resizing
self.btn_add = QPushButton("Add", clicked=self.onAddButton)
self.btn_delete = QPushButton("Delete", clicked=self.onDeleteButton)
hbox = QHBoxLayout()
hbox.addWidget(self.btn_add)
hbox.addWidget(self.btn_delete)
self.main_layout.addLayout(hbox)
# Define any additional behavior of the list
self.mylistWidget.itemDoubleClicked.connect(self.onClicked)
def onClicked(self, item):
QMessageBox.information(self, "Info", item.text())
@pyqtSlot()
def onAddButton(self):
"""
Opens a QLineEdit editor on the last item in the listwidget, allowing the user to edit the field.
NOTE: The user must click outside of the editor field then press Enter in order to close the editor
"""
self.mylistWidget.addItem('')
modelItem = self.mylistWidget.model().index(self.mylistWidget.count() - 1)
self.mylistWidget.openPersistentEditor(self, modelItem)
@pyqtSlot()
def onDeleteButton(self):
for item in self.mylistWidget.selectedItems():
self.mylistWidget.takeItem(self.mylistWidget.row(item))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = UNISACourses()
ex.show()
sys.exit(app.exec_())
【问题讨论】:
【参考方案1】:您将两个不正确的参数(self
和一个 QModelIndex)传递给接受 一个 QListWidgetItem 的QListWidget.openPersistentEditor
。使用QListWidget.item
方法获取项目。您还可以添加QListWidget.setCurrentItem
,以便立即选择它并准备好进行编辑。
def onAddButton(self):
self.mylistWidget.addItem('')
modelItem = self.mylistWidget.item(self.mylistWidget.count() - 1)
self.mylistWidget.openPersistentEditor(modelItem)
self.mylistWidget.setCurrentItem(modelItem)
同样的修复:
def keyPressEvent(self, event):
if event.key() == Qt.Key_Return:
print("Closing any persistent editor")
self.closePersistentEditor(self.item(self.count() - 1))
else:
super().keyPressEvent(event)
Qt.Key_Return
的 Qt 命名空间类也在 QtCore 模块中。
from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5 import QtGui
【讨论】:
以上是关于用于 openPersitentEditor 的 PyQt5 TypeError的主要内容,如果未能解决你的问题,请参考以下文章