PyQt QTableView嵌入QCheckBox

本文详细介绍了如何使用PyQt在QTableView中嵌入QCheckBox,并通过自定义QItemDelegate来实现CheckBox居中显示的效果,提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  原文链接:PyQt QTableView嵌入QCheckBox

  关联文章:PyQt QTableView嵌入QComboBox

  Qt里在QTableView中嵌入QCheckBox挺简单,用QItemDelegate就可以很方便地实现,不过要想让CheckBox居中则有点麻烦,好在稍稍做一些处理就可以实现,下面以PyQt为例(提示:一个CheckBoxDelegate可以用于多个列):

#coding=utf-8

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class CheckBoxDelegate(QItemDelegate):

  def __init__(self, parent=None):
    QItemDelegate.__init__(self, parent)
    self.chkboxSize = 19 #?!

  def createEditor(self, parent, option, index):
    chkbox = QCheckBox(parent)
    chkbox.setText('')
    chkbox.setTristate(False) #只用两个状态
    left = option.rect.x() + (option.rect.width() - self.chkboxSize) / 2
    top  = option.rect.y() + (option.rect.height() - self.chkboxSize) / 2
    chkbox.setGeometry(left, top, self.chkboxSize, self.chkboxSize)
    return chkbox

  def paint(self, painter, option, index):
    value = index.data().toBool()
    opt = QStyleOptionButton()
    opt.state |= QStyle.State_Enabled | (QStyle.State_On if value else QStyle.State_Off)
    opt.text = ''
    left = option.rect.x() + (option.rect.width() - self.chkboxSize) / 2
    top  = option.rect.y() + (option.rect.height() - self.chkboxSize) / 2
    opt.rect = QRect(left, top, self.chkboxSize, self.chkboxSize)
    QApplication.style().drawControl(QStyle.CE_CheckBox, opt, painter)

  def updateEditorGeometry(self, editor, option, index):
    pass

###############################################################################

if __name__ == '__main__':

  import sys

  app = QApplication(sys.argv)

  table = QTableView()
  model = QStandardItemModel(3, 3, table)
  model.setHorizontalHeaderLabels(['Name', 'Description', 'Animal?'])
  model.setData(model.index(0, 0, QModelIndex()), QVariant('Squirrel'))
  model.setData(model.index(0, 1, QModelIndex()), QVariant(u'可爱的松树精灵'))
  model.setData(model.index(0, 2, QModelIndex()), QVariant(True))
  model.setData(model.index(1, 0, QModelIndex()), QVariant('Soybean'))
  model.setData(model.index(1, 1, QModelIndex()), QVariant(u'他站在田野里吹风'))
  model.setData(model.index(1, 2, QModelIndex()), QVariant(False))
  table.setModel(model)
  table.setItemDelegateForColumn(2, CheckBoxDelegate(table))
  table.resizeColumnToContents(1)
  table.horizontalHeader().setStretchLastSection(True)
  table.setGeometry(80, 20, 400, 300)
  table.setWindowTitle('Grid + CheckBox Testing')
  table.show()

  sys.exit(app.exec_())
  下面是在Ubuntu中的效果图:

Ubuntu下的效果图

### 实现PyQt5 QTableWidget第一列添加QCheckBox控件 要在PyQt5中的`QTableWidget`的第一列添加`QCheckBox`控件,可以通过创建自定义的小部件并将该小部件放置到表格的单元格中来完成。以下是详细的实现方式: #### 创建带有QCheckBox的单元格 通过继承`QWidget`类并嵌入`QCheckBox`,可以构建一个包含复选框的小部件。随后使用`setCellWidget()`方法将其分配给特定的单元格。 ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidget, QWidget, QVBoxLayout, QCheckBox class CheckBoxWidget(QWidget): def __init__(self, checked=False, parent=None): super(CheckBoxWidget, self).__init__(parent) layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) # 移除布局边距 self.checkBox = QCheckBox() self.checkBox.setChecked(checked) layout.addWidget(self.checkBox) self.setLayout(layout) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("QTableWidget with QCheckBox") self.resize(600, 400) self.tableWidget = QTableWidget(5, 3) # 初始化具有5行3列的表格 self.setCentralWidget(self.tableWidget) # 添加复选框到第一列 for row in range(self.tableWidget.rowCount()): checkBoxWidget = CheckBoxWidget(False) # 可以传递初始状态 True/False self.tableWidget.setCellWidget(row, 0, checkBoxWidget) # 将复选框放在第0列 if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_() ``` 此代码片段展示了如何为每一行的第一个单元格动态添加`QCheckBox`控件[^2]。具体来说,它利用了一个辅助类`CheckBoxWidget`封装了`QCheckBox`逻辑,并通过`setCellWidget()`函数将这些对象绑定至目标位置。 #### 获取QCheckBox的状态 如果需要访问某个特定行内的`QCheckBox`实例及其当前状态,则可通过如下方式进行检索: ```python def get_check_box_state(self, row_index): widget = self.tableWidget.cellWidget(row_index, 0) if isinstance(widget, CheckBoxWidget): return widget.checkBox.isChecked() return False ``` 这里假设每行第一个单元格都已设置了上述类型的组件;如果不是这种情况则需额外判断是否存在有效数据再执行转换操作[^1]。 #### 调整样式使QCheckBox居中显示 为了让复选框在单元格内部水平垂直方向均处于中心位置,可以在初始化阶段调整其父级容器(即`CheckBoxWidget`)的相关参数或者直接修改CSS样式表规则[^4]: ```css QCheckBox { margin:auto; } ``` ### 注意事项 当向大量行数较多的大规模表格插入复杂子项时可能会引起性能下降问题,在这种情况下考虑改用更高效的模型视图架构如`QTableView`配合代理模式渲染会更加合适一些[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值