gpt4 book ai didi

python - PySide/PyQt4 : adding a checkbox to QTableWidget Horizontal (column) Header

转载 作者:太空狗 更新时间:2023-10-30 01:03:17 26 4
gpt4 key购买 nike

我正在尝试在我的表格小部件的水平(列)标题中放置一个复选框。基于其他帖子 here (因为基本对象类型相同),我试过这个:

item = QtGui.QTableWidgetItem()
item.setCheckState(QtCore.Qt.Checked)
self.tableWidget.setHorizontalHeaderItem(1, item)

我也试过这个:

self.tableWidget.horizontalHeaderItem(1).setCheckState(QtCore.Qt.Checked)

这些都不会在水平标题中产生复选框。不胜感激。

最佳答案

它不是那么漂亮,但解决这个问题的方法是 posted in a FAQqt-project.org 上网站。

我已经针对 Python 调整了解决方案,并进行了评论中建议的一些更改。

from PyQt4.QtCore import Qt, QRect
from PyQt4.QtGui import QTableWidget, QApplication, QHeaderView, QStyleOptionButton, QStyle

import sys

class MyHeader(QHeaderView):

isOn = False

def __init__(self, orientation, parent=None):
QHeaderView.__init__(self, orientation, parent)

def paintSection(self, painter, rect, logicalIndex):
painter.save()
QHeaderView.paintSection(self, painter, rect, logicalIndex)
painter.restore()

if logicalIndex == 0:
option = QStyleOptionButton()
option.rect = QRect(10, 10, 10, 10)
if self.isOn:
option.state = QStyle.State_On
else:
option.state = QStyle.State_Off
self.style().drawControl(QStyle.CE_CheckBox, option, painter)

def mousePressEvent(self, event):
self.isOn = not self.isOn
self.updateSection(0)
QHeaderView.mousePressEvent(self, event)

class MyTable(QTableWidget):
def __init__(self):
QTableWidget.__init__(self, 3, 3)

myHeader = MyHeader(Qt.Horizontal, self)
self.setHorizontalHeader(myHeader)

if __name__ == '__main__':
app = QApplication(sys.argv)
myTable = MyTable()
myTable.show()
sys.exit(app.exec_())

关于python - PySide/PyQt4 : adding a checkbox to QTableWidget Horizontal (column) Header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9744975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com