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

查看:4206
本文介绍了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)

其中 c $ c>是我的 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

测试

test the checkedState of the clicked item in the handler

如果该项目是已选中的项目,请按checkState> checkedState 已选中,请添加其 到列表

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天全站免登陆