只是为了一项运动,我正在玩@ekhumoro的演示代码(原始 Qt4 代码的所有功劳归他所有),他在其中插入了一行新的小部件到. 我将代码移植到 Qt5 并开始向标题添加不同的小部件。a , , 空格 ( ) 和 a没有问题。QLineEdit
QHeaderview
QTableView
QComboBox
QCheckBox
QWidget
QPushButton
但是,当我创建一个包含QWidget
aQHBoxLayout
和 a 的组合时QPushButton
(它是带有“=”符号的组合,在“三”列中)和一个QLineEdit
. 所有控件都链接到相关插槽并且运行良好,包括QLineEdit
来自第三列中的组合字段,但QPushButton
来自该组合小部件的除外。插槽 def 应该在<|=|> 值之间ChangeIntButtonSymbol(self)
循环按钮。Text
我总是得到一个错误:
AttributeError: 'FilterHeader' object has no attribute 'text'
这表明,与其他情况不同,这里父(由 检索self.sender()
)小部件的上下文是不同的,def 接收FilterHeader
类作为父类而不是btn
. 我还尝试使用 lambda 传递参数:
self.btn.clicked.connect(lambda: self.changebuttonsymbol.emit(self.btn))
...但结果完全相同(错误中的措辞不同)。
显然,我并没有完全了解这个QHeaderView
扩展的架构并犯了一些基本错误。完整的演示如下,单击“=”按钮时会出现问题,感谢任何解决方案或提示。
import sys
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QHeaderView, QWidget, QLineEdit, QApplication, QTableView, QVBoxLayout,QHBoxLayout, QLineEdit, QComboBox, QPushButton, QCheckBox
from PyQt5.QtCore import pyqtSignal
class FilterHeader(QHeaderView):
filterActivated = QtCore.pyqtSignal()
changebuttonsymbol = QtCore.pyqtSignal()
def __init__(self, parent):
super().__init__(QtCore.Qt.Horizontal, parent)
self._editors = []
self._padding = 4
self.setStretchLastSection(True)
#self.setResizeMode(QHeaderView.Stretch)
self.setDefaultAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
self.setSortIndicatorShown(False)
self.sectionResized.connect(self.adjustPositions)
parent.horizontalScrollBar().valueChanged.connect(self.adjustPositions)
def setFilterBoxes(self, count):
while self._editors:
editor = self._editors.pop()
editor.deleteLater()
for index in range(count):
if index == 1: # Empty
editor = QWidget()
elif index == 2: # Number filter (>|=|<)
editor = QWidget(self.parent())
edlay = QHBoxLayout()
edlay.setContentsMargins(0, 0, 0, 0)
edlay.setSpacing(0)
self.btn = QPushButton()
self.btn.setText("=")
self.btn.setFixedWidth(20)
#self.btn.clicked.connect(lambda: self.changebuttonsymbol.emit(self.btn))
self.btn.clicked.connect(self.changebuttonsymbol.emit)
#btn.setViewportMargins(0, 0, 0, 0)
linee = QLineEdit(self.parent())
linee.setPlaceholderText('Filter')
linee.returnPressed.connect(self.filterActivated.emit)
#linee.setViewportMargins(0, 0, 0, 0)
edlay.addWidget(self.btn)
edlay.addWidget(linee)
editor.setLayout(edlay)
elif index == 3:
editor = QComboBox(self.parent())
editor.addItems(["", "Combo", "One", "Two", "Three"])
editor.currentIndexChanged.connect(self.filterActivated.emit)
elif index == 4:
editor = QPushButton(self.parent())
editor.clicked.connect(self.filterActivated.emit)
editor.setText("Button")
elif index == 5:
editor = QCheckBox(self.parent())
editor.clicked.connect(self.filterActivated.emit)
editor.setTristate(True)
editor.setCheckState(1)
editor.setText("CheckBox")
else: # string filter
editor = QLineEdit(self.parent())
editor.setPlaceholderText('Filter')
editor.returnPressed.connect(self.filterActivated.emit)
self._editors.append(editor)
self.adjustPositions()
def sizeHint(self):
size = super().sizeHint()
if self._editors:
height = self._editors[0].sizeHint().height()
size.setHeight(size.height() + height + self._padding)
return size
def updateGeometries(self):
if self._editors:
height = self._editors[0].sizeHint().height()
self.setViewportMargins(0, 0, 0, height + self._padding)
else:
self.setViewportMargins(0, 0, 0, 0)
super().updateGeometries()
self.adjustPositions()
def adjustPositions(self):
for index, editor in enumerate(self._editors):
height = editor.sizeHint().height()
CompensateY = 0
CompensateX = 0
if self._editors[index].__class__.__name__ == "QComboBox":
CompensateY = +2
elif self._editors[index].__class__.__name__ == "QWidget":
CompensateY = -1
elif self._editors[index].__class__.__name__ == "QPushButton":
CompensateY = -1
elif self._editors[index].__class__.__name__ == "QCheckBox":
CompensateY = 4
CompensateX = 4
editor.move( self.sectionPosition(index) - self.offset() + 1 + CompensateX, height + (self._padding // 2) + 2 + CompensateY)
editor.resize(self.sectionSize(index), height)
def filterText(self, index):
if 0 <= index < len(self._editors):
if self._editors[index].__class__.__name__ == "QLineEdit":
return self._editors[index].text()
return ''
def setFilterText(self, index, text):
if 0 <= index < len(self._editors):
self._editors[index].setText(text)
def clearFilters(self):
for editor in self._editors:
editor.clear()
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.view = QTableView()
layout = QVBoxLayout(self)
layout.addWidget(self.view)
header = FilterHeader(self.view)
self.view.setHorizontalHeader(header)
model = QtGui.QStandardItemModel(self.view)
model.setHorizontalHeaderLabels('One Two Three Four Five Six Seven'.split())
self.view.setModel(model)
header.setFilterBoxes(model.columnCount())
header.filterActivated.connect(self.handleFilterActivated)
header.changebuttonsymbol.connect(self.ChangeIntButtonSymbol)
def handleFilterActivated(self):
header = self.view.horizontalHeader()
for index in range(header.count()):
if index != 4:
print((index, header.filterText(index)))
else:
print("Button")
def ChangeIntButtonSymbol(self):
print("Int button triggered")
nbtn = self.sender()
print(str(nbtn))
if nbtn.text() == "=":
nbtn.setText(">")
elif nbtn.text() == ">":
nbtn.setText("<")
else:
nbtn.setText("=")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.setGeometry(800, 100, 600, 300)
window.show()
sys.exit(app.exec_())