在自定义树视图中显示QComboBox向下箭头,可以通过以下步骤实现:
以下是一个示例代码:
from PyQt5.QtWidgets import QTreeView, QStyledItemDelegate, QComboBox, QApplication
from PyQt5.QtCore import Qt
class CustomTreeView(QTreeView):
def __init__(self):
super().__init__()
self.setItemDelegate(CustomDelegate(self))
class CustomDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
if index.column() == 0: # 判断需要显示QComboBox的列
# 绘制QComboBox外观
combo_rect = option.rect
combo_rect.setWidth(combo_rect.width() - 20) # 减去箭头的宽度
QStyleOptionComboBox().rect = combo_rect
self.parent().style().drawComplexControl(QStyle.CC_ComboBox, QStyleOptionComboBox(), painter)
# 绘制箭头
arrow_rect = option.rect
arrow_rect.setX(arrow_rect.x() + arrow_rect.width() - 20) # 箭头的起始位置
arrow_rect.setWidth(20) # 箭头的宽度
QStyleOptionComboBox().rect = arrow_rect
self.parent().style().drawPrimitive(QStyle.PE_IndicatorArrowDown, QStyleOptionComboBox(), painter)
# 绘制文本
text_rect = option.rect
text_rect.setWidth(text_rect.width() - 20) # 减去箭头的宽度
text = index.data(Qt.DisplayRole)
painter.drawText(text_rect, Qt.AlignVCenter, text)
else:
super().paint(painter, option, index)
if __name__ == '__main__':
app = QApplication([])
tree_view = CustomTreeView()
tree_view.show()
app.exec_()
这个示例代码演示了如何在自定义树视图中显示带有向下箭头的QComboBox。你可以根据自己的需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云