V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Henrysun
V2EX  ›  Python

请教一下各位佬, Pyqt5 中 QComboBox 选择框的文本怎么居中

  •  
  •   Henrysun · 1 天前 · 169 次点击
    问了 gpt ,试了很多办法,目前只能让下拉选项居中,已选择的框始终不能居中
    iorilu
        1
    iorilu  
       1 天前
    你文本前弄几个空格不行吗
    Henrysun
        2
    Henrysun  
    OP
       1 天前
    找到办法了,通过重构 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())
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2738 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 17ms · UTC 05:49 · PVG 13:49 · LAX 21:49 · JFK 00:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.