如何在 QComboBox 中居中文本? [英] How to center text in QComboBox?

查看:216
本文介绍了如何在 QComboBox 中居中文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过使用 QComboBox 的 model(),但没有明显的成功.我想知道是否可以在 QCombobox 的中心对齐文本.除了文本对齐之外,似乎该项目的字体不受更改其 PointSize 的影响....

I've tried using QComboBox's model() with no apparent success. I wonder if it would be possible to align a text at the center of QCombobox. Aside from text alignment it seems the item's font is not effected by changing its PointSize....

    combo=QtGui.QComboBox()
    comboModel=combo.model()

    for name in ['one','two','three']:
        item = QtGui.QStandardItem(name)
        itemFont = item.font()
        itemFont.setPointSize(8)
        item.setFont(itemFont)
        # item.setAlignment(QtCore.Qt.AlignCenter)
        comboModel.appendRow(item)

推荐答案

您可以使用 setAlignment 方法:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.combo = QtGui.QComboBox()
        self.combo.setEditable(True)
        self.combo.lineEdit().setAlignment(QtCore.Qt.AlignCenter)
        self.combo.addItems('One Two Three Four Five'.split())
        layout.addWidget(self.combo)


if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

这篇关于如何在 QComboBox 中居中文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆