PyQT QTreeview + QPushButton / QCombobox信号

时间:2016-07-11 13:56:29

标签: python pyqt pyqt4 qtreeview

我正在尝试加载一堆文件,这些文件是dics,然后从dics中加载数组到我的QTreeView,然后就可以编辑这些dics了。我在连接信号时遇到问题,因为它将所有按钮连接到1个数据 - 最后创建一个。如果我从1个dict加载20个数组,我应该能够点击每个数组并打印它的名字。现在它只打印最后添加的名称。

以下是代码:

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  

以下是QTreeView的样子,每个作业可以有数百个job_names,可以有数百个Job_01等等......它的一个大清单: - ) HDD上的1个文件创建1个父项,用于创建子作业。

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...

1 个答案:

答案 0 :(得分:0)

Use partial :

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