Search code examples
pythonpyqtpyqt4qtreeview

PyQT QTreeview + QPushButton/QCombobox signals


I'm trying to load a serries of files that are dics and then load arrays from within dics to my QTreeView and then be able to edit these dics. I have a problem when it comes to connecting signal as it connects all buttons to 1 data - last created one. If I load 20 arrays from 1 dict I should be able to click on each array and print its name. Right now it just prints last added name.

Here is the code:

def add_data(self):
        for subdir, dirs, files in os.walk(self.dat_folder):
            for file_inx, file_name in enumerate(files):
                ''' loading file '''
                ''' creating data'''

                if len(data[1]) >0:
                    #file_inx = file_inx + 1 # not sure if I need this tbd.
                    job = QStandardItem(project_name)
                    self.model.setItem(file_inx,0,job)
                    self.model.setItem(file_inx, 1, QStandardItem(project_time_day+"  "+project_time_time))
                    for inx, layers in enumerate(data[1]):
                        child1 = QStandardItem(layers["Name"])
                        child2 = QStandardItem("Push Button or Combobox or QCheckBox")
                        job.insertRow(inx,[child1, child2])
                        b=QPushButton("TestPrint"+str(inx))
                        b.clicked.connect(lambda: self.printData(child1.text(),layers["Name"]))

                        a = self.model.index(file_inx, 0) #find parent
                        i = a.child(inx,7) # find parented location
                        self.tv_job_list.setIndexWidget(i,b) # replace child2 with QPushButton - b 

    def printData(self,value,name):
        print value,name  

Here is how the QTreeView looks like, each job can have hundreds of job_names and there can be hundreds of Job_01 etc etc... Its a big list :- ) 1 file on HDD creates 1 parent that create child jobs.

Parent > JOB_01
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     ... x 1000 Childs...


Parent > JOB_01
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     Child > Job_Name | Job_Submit_Date | QComboBox | QPushButton | QCheckBox
     ... x 1000 Childs...

Solution

  • Use partial :

      b = QPushButton(str(inx))
      b.clicked.connect(partial(self.printData,child1.text()))