Henrysun
244 天前
找到办法了,通过重构 QComboBox 类实现。
然后原本的 self.select_combo = QComboBox()改为重构后的 self.select_combo = CenteredComboBox()创建
现提供给大家参考
# --------------------------
class CenteredComboBox(QComboBox):
def paintEvent(self, event):
painter = QStylePainter(self)
option = QStyleOptionComboBox()
self.initStyleOption(option)
# 绘制控件框架
painter.drawComplexControl(QStyle.CC_ComboBox, option)
# 手动居中文本
text_rect = self.style().subControlRect(
QStyle.CC_ComboBox, option, QStyle.SC_ComboBoxEditField)
text_rect.adjust(25, 0, 0, 0)
painter.drawText(text_rect, Qt.AlignCenter, self.currentText())