如何在 PyQt5 中返回 QlistWidget 中项目的值

Posted

技术标签:

【中文标题】如何在 PyQt5 中返回 QlistWidget 中项目的值【英文标题】:how to return the value of item in the QlistWidget in PyQt5 【发布时间】:2020-11-30 11:50:12 【问题描述】:

我在 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_())
  

【问题讨论】:

【参考方案1】:

问题很简单,假设在变量“checked”中要保存已经被检查的QListWidgetItem。让我们通过一个例子来分析发生了什么:假设用户选择了第一个选项,然后“checked”最初将为空,因此选中的项目被比较并添加到列表中,但后来检查的是 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)

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

【讨论】:

以上是关于如何在 PyQt5 中返回 QlistWidget 中项目的值的主要内容,如果未能解决你的问题,请参考以下文章

如何在pyqt5 QlistWidget中选择多个项目并打印它们[重复]

在PyQt5中的QTreeWidget和QListWidget之间拖动项目?

PyQt5 QListWidget自定义项目[重复]

pyqt5 qlistwidget 对大文本数据的性能

PyQt5 检查项目是不是已经在 QListWidget

获取 QListWidget PyQt5 中所选项目的名称