0
votes

How to return or print all the checked items in a QlistWidget?

I am able to print the selected items but what i want is to print the checked items

So i want to check if the item is checked if yes the system append the checked item to the list and at the end it prints this list.

The code below includes the GUI part and the functions:

  • load file
  • display header
  • display checked items

code:

    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_())  
1

1 Answers

1
votes

You need to cycle through all items and check for their 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))