如何解决OpenPersitentEditor的PyQt5 TypeError
大家好,我正在用PYQT5做一些考试准备,我从工作簿中的练习中创建了一个应用程序,其中必须使它显示课程列表,当您单击它们时,它会打开一个带有课程名称的消息框还有一个按钮,以便用户可以将课程添加到列表中。支持使用添加按钮在listWidget的最后一项上打开QlineEdit,因此用户可以编辑字段,但是我一直收到TypeError消息:
第67行,位于onAddButton中 self.mylistWidget.openPersistentEditor(self,modelItem) TypeError:openPersistentEditor(self,QListWidgetItem):参数1具有意外类型'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,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_())
解决方法
您正在将两个错误的参数(self
和QModelIndex)传递给QListWidget.openPersistentEditor
,该参数接受一个 QListWidgetItem。使用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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。