gpt4 book ai didi

python - 如何在QHeaderView 和QTableView 之间注入(inject)widgets?

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:39 24 4
gpt4 key购买 nike

我想在 QHeaderViewQTableView 的其余部分之间显示小部件,如下面的示例图片(使用 Photoshop 创建),因为这似乎是启用过滤列输入的自然方式。


有没有人知道如何在中间注入(inject)小部件?


enter image description here

最佳答案

下面是我为自己的一个项目编写的 FilterHeader 类的演示。您可能需要调整它以满足您自己的需要,但它应该已经完成​​了您想要的大部分工作。过滤器框周围的填充不太可能在所有平台上都以相同的方式工作,因此您可能需要调整 adjustPositions 方法中的代码。

screenshot

import sys
from PyQt4 import QtCore, QtGui

class FilterHeader(QtGui.QHeaderView):
filterActivated = QtCore.pyqtSignal()

def __init__(self, parent):
super().__init__(QtCore.Qt.Horizontal, parent)
self._editors = []
self._padding = 4
self.setStretchLastSection(True)
self.setResizeMode(QtGui.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):
editor = QtGui.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()
editor.move(
self.sectionPosition(index) - self.offset() + 2,
height + (self._padding // 2))
editor.resize(self.sectionSize(index), height)

def filterText(self, index):
if 0 <= index < len(self._editors):
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(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.view = QtGui.QTableView()
layout = QtGui.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'.split())
self.view.setModel(model)
header.setFilterBoxes(model.columnCount())
header.filterActivated.connect(self.handleFilterActivated)

def handleFilterActivated(self):
header = self.view.horizontalHeader()
for index in range(header.count()):
print((index, header.filterText(index)))


if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 600, 300)
window.show()
sys.exit(app.exec_())

关于python - 如何在QHeaderView 和QTableView 之间注入(inject)widgets?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44343738/

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