如何在 qlistwidget 中打印所有选中项目的列表。在 python
how to print a list of all the checked items in qlistwidget. in python
如何return或打印 QlistWidget 中所有选中的项目?
我可以打印选中的项目,但我想要打印选中的项目
所以我想检查该项目是否被选中,如果是,系统将选中的项目附加到列表中,最后它打印这个列表。
以下代码包括GUI部分和功能:
- 加载文件
- 显示header
- 显示选中的项目
代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import pandas as pd
from PandasModel import PandasModel
import seaborn as sns
import cufflinks as cf
import plotly
import plotly.offline as py
import plotly.express as px
import plotly.graph_objs as go
from plotly.offline.offline import iplot
cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=None)
self.gridLayout_3 = QtWidgets.QGridLayout(self)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.pathLE = QtWidgets.QLineEdit(self)
self.gridLayout_2.addWidget(self.pathLE, 0, 1, 1, 1)
self.loadBtn = QtWidgets.QPushButton("Select File", self)
self.loadBtn.clicked.connect(self.loadFile)
self.gridLayout_2.addWidget(self.loadBtn, 0, 0, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 4)
self.displayHeader = QtWidgets.QPushButton("Display header", self)
self.displayHeader.clicked.connect(lambda: self.print_df_header(self.df))
self.gridLayout.addWidget(self.displayHeader, 2, 0, 1, 1)
self.displayDF_checked_header = QtWidgets.QPushButton("Print checked item", self)
self.displayDF_checked_header.clicked.connect(lambda: self.setItems(self.df))
self.gridLayout.addWidget(self.displayDF_checked_header, 5, 0, 1, 1)
self.header_list = QtWidgets.QListWidget(self)
self.header_list.setObjectName("listWidget")
self.header_list.itemSelectionChanged.connect(self.selectionChanged)
self.gridLayout_3.addWidget(self.header_list, 1, 2, 1, 1)
self.pandasTv = QtWidgets.QTableView(self)
self.verticalLayout.addWidget(self.pandasTv)
self.pandasTv.setSortingEnabled(True)
def loadFile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", "", "CSV Files (*.csv)");
self.pathLE.setText(fileName)
df = pd.read_csv(fileName)
model = PandasModel(df)
self.pandasTv.setModel(model)
self.df = df
def print_df_header(self,df):
savelist = list(self.df)
for item in savelist:
qitem = QtWidgets.QListWidgetItem ( )
qitem.setText ( item )
qitem.setCheckState ( QtCore.Qt.Unchecked )
self.header_list.addItem ( qitem )
print(savelist)
def setItems(self, item):
self.items=[]
if item.checkState() == QtCore.Qt.Checked:
self.items.append(item)
print(self.items.append(item))
if item.checkState() == QtCore.Qt.Unchecked:
self.items.remove(item)
# this function allow to print the selected items
def selectionChanged(self):
print("Selected items: ", self.header_list.selectedItems())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
您需要遍历所有项目并检查它们 checkState()
:
def selectionChanged(self):
checked = []
for row in range(self.header_list.count()):
item = self.header_list.item(row)
if item.checkState():
checked.append(item)
print("Checked items: ", ", ".join(i.text() for i in checked))
如何return或打印 QlistWidget 中所有选中的项目?
我可以打印选中的项目,但我想要打印选中的项目
所以我想检查该项目是否被选中,如果是,系统将选中的项目附加到列表中,最后它打印这个列表。
以下代码包括GUI部分和功能:
- 加载文件
- 显示header
- 显示选中的项目
代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import pandas as pd
from PandasModel import PandasModel
import seaborn as sns
import cufflinks as cf
import plotly
import plotly.offline as py
import plotly.express as px
import plotly.graph_objs as go
from plotly.offline.offline import iplot
cf.go_offline()
cf.set_config_file(offline=False, world_readable=True)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=None)
self.gridLayout_3 = QtWidgets.QGridLayout(self)
self.gridLayout_3.setObjectName("gridLayout_3")
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.pathLE = QtWidgets.QLineEdit(self)
self.gridLayout_2.addWidget(self.pathLE, 0, 1, 1, 1)
self.loadBtn = QtWidgets.QPushButton("Select File", self)
self.loadBtn.clicked.connect(self.loadFile)
self.gridLayout_2.addWidget(self.loadBtn, 0, 0, 1, 1)
self.gridLayout_3.addLayout(self.gridLayout_2, 0, 0, 1, 4)
self.displayHeader = QtWidgets.QPushButton("Display header", self)
self.displayHeader.clicked.connect(lambda: self.print_df_header(self.df))
self.gridLayout.addWidget(self.displayHeader, 2, 0, 1, 1)
self.displayDF_checked_header = QtWidgets.QPushButton("Print checked item", self)
self.displayDF_checked_header.clicked.connect(lambda: self.setItems(self.df))
self.gridLayout.addWidget(self.displayDF_checked_header, 5, 0, 1, 1)
self.header_list = QtWidgets.QListWidget(self)
self.header_list.setObjectName("listWidget")
self.header_list.itemSelectionChanged.connect(self.selectionChanged)
self.gridLayout_3.addWidget(self.header_list, 1, 2, 1, 1)
self.pandasTv = QtWidgets.QTableView(self)
self.verticalLayout.addWidget(self.pandasTv)
self.pandasTv.setSortingEnabled(True)
def loadFile(self):
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open File", "", "CSV Files (*.csv)");
self.pathLE.setText(fileName)
df = pd.read_csv(fileName)
model = PandasModel(df)
self.pandasTv.setModel(model)
self.df = df
def print_df_header(self,df):
savelist = list(self.df)
for item in savelist:
qitem = QtWidgets.QListWidgetItem ( )
qitem.setText ( item )
qitem.setCheckState ( QtCore.Qt.Unchecked )
self.header_list.addItem ( qitem )
print(savelist)
def setItems(self, item):
self.items=[]
if item.checkState() == QtCore.Qt.Checked:
self.items.append(item)
print(self.items.append(item))
if item.checkState() == QtCore.Qt.Unchecked:
self.items.remove(item)
# this function allow to print the selected items
def selectionChanged(self):
print("Selected items: ", self.header_list.selectedItems())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
您需要遍历所有项目并检查它们 checkState()
:
def selectionChanged(self):
checked = []
for row in range(self.header_list.count()):
item = self.header_list.item(row)
if item.checkState():
checked.append(item)
print("Checked items: ", ", ".join(i.text() for i in checked))