Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取QTableWidget PyQT中checkbox cellWidget的状态_Python_Qt_Pyqt_Qtablewidget_Qcheckbox - Fatal编程技术网

Python 获取QTableWidget PyQT中checkbox cellWidget的状态

Python 获取QTableWidget PyQT中checkbox cellWidget的状态,python,qt,pyqt,qtablewidget,qcheckbox,Python,Qt,Pyqt,Qtablewidget,Qcheckbox,我为QGIS开发python插件。在这个插件中,我创建了一个有3列的QTableWidget。这些列是QCheckbox、QTableWidgetItem和QComboBox。我想检索这3列中包含的值。目前,我设法获得了QComboBox和QTableWidgetItem的值,但似乎无法获得QCheckBox的值 liste = ['Carte 1','Carte 2','Carte 3','Carte 4','Carte 5','Carte 6'] combo_box_opti

我为QGIS开发python插件。在这个插件中,我创建了一个有3列的QTableWidget。这些列是QCheckbox、QTableWidgetItem和QComboBox。我想检索这3列中包含的值。目前,我设法获得了QComboBox和QTableWidgetItem的值,但似乎无法获得QCheckBox的值

liste = ['Carte 1','Carte 2','Carte 3','Carte 4','Carte 5','Carte 6']
        combo_box_options = ["A4 Paysage","A4 Portrait", "A3 Paysage","A3 Portrait"]
        self.dlg_format = Dialog_format()
        self.dlg_format.tableWidget.setRowCount(len(liste))

        for index in range(len(liste)):
            item = QTableWidgetItem(liste[index])
            self.dlg_format.tableWidget.setItem(index, 1, item)
            self.dlg_format.tableWidget.setColumnWidth(0, 20)
            self.dlg_format.tableWidget.setColumnWidth(1, 350)

            combo = QComboBox()
            for t in combo_box_options:
                combo.addItem(t)
            self.dlg_format.tableWidget.setCellWidget(index, 2, combo)

            widget = QWidget()
            checkbox = QCheckBox()
            checkbox.setCheckState(Qt.Checked)
            playout = QHBoxLayout(widget)
            playout.addWidget(checkbox)
            playout.setAlignment(Qt.AlignCenter)
            playout.setContentsMargins(0,0,0,0)
            widget.setLayout(playout)
            self.dlg_format.tableWidget.setCellWidget(index, 0, widget)

        self.dlg_format.show()
        result = self.dlg_format.exec_()

        if result:
            for index in range(len(liste)):
                text = self.dlg_format.tableWidget.item(index, 1).text()
                format = self.dlg_format.tableWidget.cellWidget(index, 2).currentText()
                check = self.dlg_format.tableWidget.cellWidget(index, 0).checkState() #Does not work


QWidget是设置为cell小部件的,而不是复选框,并且该小部件显然没有
checkState
属性

这种情况有多种可能性

使复选框成为小部件的属性:

widget=QWidget()
widget.checkbox=QCheckBox()
playout.addWidget(widget.checkbox)
# ...
check=self.dlg\u format.tableWidget.cellWidget(索引,0).checkbox.checkState()
将复选框的
checkState
函数作为小部件的参考(注意:没有括号!),以便您可以使用现有的
cellWidget(索引,0)访问它。checkState()

checkbox=QCheckBox()
widget.checkState=checkbox.checkState
由于所有操作都发生在同一范围内(函数),因此您可以完全忽略
cellWidget
,并使用包含小部件的元组列表:

    widgets = []
    for index in range(len(liste)):
        # ...
        widgets.append((item, combo, checkbox))
    # ...
    if result:
        for item, combo, checkbox in widgets:
            text = item.text()
            format = combo.currentText()
            check = checkbox.checkState()
请注意:

  • 返回一个枚举,这将导致复选框的2
    Qt.Checked
    );如果需要布尔值,请使用
  • 您可以使用
    枚举
    而不是
    范围
    ,因为您仍在迭代列表项:
    对于索引,枚举中的文本(liste):
  • 如果您不需要添加项目数据,并且组合的内容总是相同的,只需使用
    combo.addItems(组合框选项)
  • 为每个循环设置列宽是没有意义的,只需在
    for
    循环外执行一次即可
  • 如果使用
    QHBoxLayout(widget)
    则不需要
    widget.setLayout(playout)
    ,因为布局上的widget参数已经在小部件上设置了该布局
  • 创建实例属性是为了使它们持久化(它确保它们不会被垃圾收集,并允许将来访问);从您的代码来看,在该函数返回后,您似乎不太可能使用该对话框实例,因此不需要将其作为实例的成员(
    self.dlg_format
    ),并且会不必要地占用资源:对话框即使在关闭后也会保留在内存中,一旦再次创建,就会被删除和覆盖;只需将其设置为局部变量(
    dlg_format=Dialog_format()

您添加的不是QCheckBox作为单元格小部件,而是包含它的QWidget(显然没有
checkState()
属性。是否需要该父QWidget?是的,此父QWidget是必需的,因为它允许对齐单元格中心的复选框