【问题标题】:PyQT QTreeview + QPushButton/QCombobox signalsPyQT QTreeview + QPushButton/QCombobox 信号
【发布时间】:2016-11-13 12:15:29
【问题描述】:

我正在尝试加载一系列 dics 文件,然后将 dics 中的数组加载到我的 QTreeView 中,然后能够编辑这些 dics。我在连接信号时遇到问题,因为它将所有按钮连接到 1 个数据 - 最后创建一个。如果我从 1 个字典中加载 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...

【问题讨论】:

  • 每次内部 for 循环循环时都会重新初始化 QPushButton,因此只有最后一个数组会显示是有道理的。您覆盖了 for 循环中的前 19 个,因此该按钮仅连接到第 20 个数组。
  • 是的,我知道这就是我面临的问题......我如何不覆盖它?
  • 将其从循环中取出,而是将按钮连接到将循环通过data[1] 并打印出数据的方法。
  • 嗯,我迷路了,如果我循环遍历整个数据列表[1],代码怎么知道我点击了哪个按钮?更不用说数据可以有数百个条目......对不起,我有点失落......我需要得到的是第 1 行条目/行条目已更改(因为它可以是组合框/复选框/),那么我可以去 dict 并在那里进行更改...我不想遍历整个数据。只是特定行的特定数据...
  • 你想要多少个按钮?如果有 20 个数据点,您想要 20 个按钮,还是想要一个可以按下以获得下一个数据点的按钮?我不太确定你要说实话

标签: python pyqt pyqt4 qtreeview


【解决方案1】:

使用部分:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-01
    • 2011-05-08
    • 2016-10-11
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多