[英]PySide/PyQt4: adding a checkbox to QTableWidget Horizontal (column) Header
我正在嘗試在tablewidget的水平(列)標題中放置一個復選框。 基於此處的另一篇文章(因為基礎對象類型相同),我已經嘗試過:
item = QtGui.QTableWidgetItem()
item.setCheckState(QtCore.Qt.Checked)
self.tableWidget.setHorizontalHeaderItem(1, item)
我也嘗試過這個:
self.tableWidget.horizontalHeaderItem(1).setCheckState(QtCore.Qt.Checked)
這些都不會在水平標題中產生一個復選框。 建議表示贊賞。
並不僅如此,但解決方案已發布在 qt-project.org網站的FAQ中 。
我已經為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_())
感謝Gary的回答,這是他的checkable標頭的修改版本,其中所有部分都可以單獨檢查
class QCheckableHeader(QHeaderView):
def __init__(self, orientation, parent=None):
QHeaderView.__init__(self, orientation, parent)
self.lisCheckboxes = []
self.sectionCountChanged.connect(self.onSectionCountChanged)
def paintSection(self, painter, rect, logicalIndex):
print "paintSection", logicalIndex
painter.save()
QHeaderView.paintSection(self, painter, rect, logicalIndex)
painter.restore()
painter.save()
painter.translate(rect.topLeft())
option = QStyleOptionButton()
option.rect = QRect(10, 10, 10, 10)
if (len(self.lisCheckboxes) != self.count()):
self.onSectionCountChanged(len(self.lisCheckboxes), self.count())
if self.lisCheckboxes[logicalIndex]:
option.state = QStyle.State_On
else:
option.state = QStyle.State_Off
self.style().drawControl(QStyle.CE_CheckBox, option, painter)
painter.restore()
def mousePressEvent(self, event):
iIdx = self.logicalIndexAt(event.pos())
self.lisCheckboxes[iIdx] = not self.lisCheckboxes[iIdx]
self.updateSection(iIdx)
QHeaderView.mousePressEvent(self, event)
@QtCore.Slot()
def onSectionCountChanged(self, oldCount, newCount):
if newCount > oldCount:
for i in range(newCount - oldCount):
self.lisCheckboxes.append(False)
else:
self.lisCheckboxes = self.lisCheckboxes[0:newCount]
希望它也可以幫助我以外的其他人:-)
我給加里·休斯(Gary Hughes)功勞是因為他實際上將復選框放到了標題部分本身,但是我想我會在發布我的更簡單解決方案之前發布,以防有人想以簡單的方式做到這一點。 這基於我在Qt Developer論壇上獲得的建議:
我將QTableWidget子類化,以使horizontalHeader()的復選框成為子控件,並在表調整大小時手動重新定位復選框:
class custom_table(QtGui.QTableWidget):
def __init__(self, parent=None):
QtGui.QTableWidget.__init__(self, parent)
self.chkbox1 = QtGui.QCheckBox(self.horizontalHeader())
def resizeEvent(self, event=None):
super().resizeEvent(event)
self.chkbox1.setGeometry(QtCore.QRect((self.columnWidth(0)/2), 2, 16, 17))
“(self.columnWidth(0)/ 2)”將復選框保留在列標題的中間。
改編自qt-blog http://blog.qt.io/blog/2014/04/11/qt-weekly-5-widgets-on-a-qheaderview/
這也可以用於將任意小部件放置到標題中
# coding=utf-8
from PySide.QtCore import Qt
from PySide.QtGui import QHeaderView, QCheckBox
from qtpy import QtCore
class CustomHeaderView(QHeaderView):
def __init__(self, parent=None):
QHeaderView.__init__(self, Qt.Horizontal, parent)
self.setMovable(True)
self.boxes = []
self.sectionResized.connect(self.handleSectionResized)
self.sectionMoved.connect(self.handleSectionMoved)
def scrollContentsBy(self, dx, dy):
super().scrollContentsBy(dx, dy)
if dx != 0:
self.fixComboPositions()
def fixComboPositions(self):
for i in range(self.count() + 1):
self.boxes[i].setGeometry(self.sectionViewportPosition(i), 0,
self.sectionSize(i) - 5, self.height())
@QtCore.Slot()
def showEvent(self, e):
for i in range(self.count() + 1):
if len(self.boxes) <= i:
self.boxes.append(QCheckBox(self))
self.boxes[i].setGeometry(self.sectionViewportPosition(i), 0,
self.sectionSize(i) - 5, self.height())
self.boxes[i].show()
super().showEvent(e)
@QtCore.Slot()
def handleSectionMoved(self, logical, oldVisualIndex, newVisualIndex):
for i in range(min(oldVisualIndex, newVisualIndex),self.count()):
logical = self.logicalIndex(i)
self.boxes[logical].setGeometry(self.sectionViewportPosition(logical), 0,
self.sectionSize(logical) - 5, self.height())
@QtCore.Slot()
def handleSectionResized(self, i):
for j in range(self.visualIndex(i),self.count()):
logical = self.logicalIndex(j)
self.boxes[logical].setGeometry(self.sectionViewportPosition(logical), 0,
self.sectionSize(logical) - 5, self.height())
self.boxes[logical].updateGeometry()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.