如何return PyQt5 中QlistWidget 中item 的值

how to return the value of item in the QlistWidget in PyQt5

我在 PyQt5 中有一个 GUI 应用程序,它在 QlistWidget 中创建带有复选框的显示项目,用户在其中检查项目并且系统打印检查的值。

问题是当用户检查项目时显示以下错误:

  print([i.text() for i in self.checked])
AttributeError: 'str' object has no attribute 'text'

但是这一行 print([i.text() for i in self.checked]) 仅在 print

中打印值

这一行是错误的:

self.checked = [i.text() for i in self.checked]

代码:

from PyQt5 import QtCore, QtGui, QtWidgets

import os
import pandas as pd
import numpy as np

import chardet




class Ui_MainWindow(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
        self.checked = []
        
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        
        self.header_list = QtWidgets.QListWidget(self.centralwidget)
        self.header_list.setMaximumSize(QtCore.QSize(120, 1667))
        self.header_list.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
        self.header_list.setObjectName("header_list")
        self.header_list.itemChanged.connect(self.selectionChanged)
        
        MainWindow.setCentralWidget(self.centralwidget)
        
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")

        self.menufile = self.menubar.addMenu("File")
        self.menufile.setObjectName("menufile")

        self.menuimportfile = QtWidgets.QAction("Import File",self.menufile)
        self.menuimportfile.setObjectName("importfile")
        self.menuimportfile.triggered.connect(lambda:self.loadFile())
        
        MainWindow.setMenuBar(self.menubar)
        
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        
        MainWindow.setStatusBar(self.statusbar)
        
        self.menufile.addAction(self.menuimportfile)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.menufile.setTitle(_translate("MainWindow", "file"))


    def loadFile(self):
        try:
            fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", "", "Excel Files (*.csv *.xls *.xlsx)");
            print(fileName)            
            name, ext = os.path.splitext(fileName)
            print("name: {} ".format(name))
            print("ext:  {} ".format(ext))
            if ext == ".csv":
                with open(fileName, 'rb') as rawdata:
                    result = chardet.detect(rawdata.read(100000))
                print(result)
                df = pd.read_csv(fileName,encoding = result["encoding"])
            elif ext == ".xls" or ext == ".xlsx":
                df = pd.read_excel(fileName)
            self.df = df

            
        #part that display items in the qlistWidget
            self.header_list.clear()
            savelist = list(self.df)
            for item in savelist:
                qitem = QtWidgets.QListWidgetItem ( ) 
                qitem.setText ( item ) 
                qitem. setFlags ( QtCore. Qt . ItemIsUserCheckable | QtCore. Qt . ItemIsEnabled ) 
                qitem.setCheckState ( QtCore.Qt.Unchecked ) 
                self.header_list.addItem ( qitem )
        
        except Exception as e:
            print("error is {}".format(e))


    def selectionChanged(self, item):
        if item.checkState():
            if item not in self.checked:
                self.checked.append(item)
        elif item in self.checked:
            self.checked.remove(item)
        print([i.text() for i in self.checked])
        self.checked = [i.text() for i in self.checked]
        print(self.checked)
        


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
  

问题很简单,假设您要在变量“checked”中保存已检查的QListWidgetItem。让我们分析一个例子会发生什么:假设用户选择了第一个选项,然后“选中”最初将为空,因此选中的项目被比较并添加到列表中,但后来选中的是 QListWidgetItem 的文本列表,这使逻辑变性了。因此,作为教训,不要对多个事物使用相同的变量:

def selectionChanged(self, item):
    if item.checkState():
        if item not in self.checked:
            self.checked.append(item)
    elif item in self.checked:
        self.checked.remove(item)

    <b>values = [i.text() for i in self.checked]
    print(values)</b>