PyQt:QTableWidget 中的复选框 [英] PyQt : Checkbox in QTableWidget

查看:74
本文介绍了PyQt:QTableWidget 中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在我的 QTableWidget

I use following code to put a checkbox in the 9th column of my QTableWidget

chkBoxItem = QtGui.QTableWidgetItem()
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)       
table.setItem(rowNo,9,chkBoxItem)

table 是我的 QtTableWidget.我需要将单击复选框的行添加到列表中.. 我到底是如何实现这一目标的?

Where table is my QtTableWidget. I need to add the row where the checkbox is clicked to a list.. how on earth do I achieve this?

亲切的问候,

推荐答案

一种方法是:

  1. 连接itemClicked 表的信号给处理程序

  1. connect the itemClicked signal of the table to a handler

测试 checkedState 在处理程序中被点击的项目

test the checkedState of the clicked item in the handler

如果项目被选中,添加它的到列表

if the item is checked, add its row to the list

示例代码:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self, rows, columns):
        QtGui.QWidget.__init__(self)
        self.table = QtGui.QTableWidget(rows, columns, self)
        for column in range(columns):
            for row in range(rows):
                item = QtGui.QTableWidgetItem('Text%d' % row)
                if row % 2:
                    item.setFlags(QtCore.Qt.ItemIsUserCheckable |
                                  QtCore.Qt.ItemIsEnabled)
                    item.setCheckState(QtCore.Qt.Unchecked)
                self.table.setItem(row, column, item)
        self.table.itemClicked.connect(self.handleItemClicked)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.table)
        self._list = []

    def handleItemClicked(self, item):
        if item.checkState() == QtCore.Qt.Checked:
            print('"%s" Checked' % item.text())
            self._list.append(item.row())
            print(self._list)
        else:
            print('"%s" Clicked' % item.text())

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(6, 3)
    window.resize(350, 300)
    window.show()
    sys.exit(app.exec_())

这篇关于PyQt:QTableWidget 中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆