0

大家好,所以我正在用 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_())
4

1 回答 1

0

您传递了两个不正确的参数(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 命名空间类Qt.Key_Return也位于 QtCore 模块中。

from PyQt5.QtCore import pyqtSlot, Qt
from PyQt5 import QtGui
于 2020-10-14T08:26:41.667 回答