亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

openPersitentEditor 的 PyQt5 類型錯誤

openPersitentEditor 的 PyQt5 類型錯誤

PHP
揚帆大魚 2023-11-09 21:11:59
大家好,我正在使用 PYQT5 做一些考試準備,我根據工作簿中的練習創建了一個應用程序,我們必須讓它顯示課程列表,當您單擊它們時,它會打開一個帶有課程名稱的消息框,也有一個按鈕,以便用戶可以將課程添加到列表中。添加按鈕應該在 listWidget 中的最后一項上打開 QlineEdit,因此用戶可以編輯該字段,但我不斷收到 TypeError 消息:第 67 行,在 onAddButton self.mylistWidget.openPersistentEditor(self, modelItem) TypeError: openPersistentEditor(self, QListWidgetItem): 參數 1 具有意外類型 'UNISACourses'import sysfrom PyQt5.QtWidgets import (QListWidget, QLineEdit, QWidget, QMessageBox, QHBoxLayout, QAbstractItemView,                             QApplication, QVBoxLayout, QPushButton, QButtonGroup)from PyQt5.QtCore import pyqtSlotfrom PyQt5 import Qt, QtGuiclass 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)
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

您傳遞了兩個不正確的參數(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


查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號