vendredi 4 septembre 2020

how to print the checked item in the QlistWidgetItem using python?

I have a python script that create a GUI app that load csv file and display its data in the Qtableview and perform some functions on it. One of the function is to extract headers column and store it in a QlistWidget with a checkbox beside each item in order to allow the user to check the selected item and print the selected one.

Until now I am able to perform all the required task the problem is just to print the 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.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)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())  



Aucun commentaire:

Enregistrer un commentaire