python - 如何在 QComboBox 中居中文本?

标签 python pyqt qcombobox

我试过使用 QComboBox 的 model() 但没有明显成功。我想知道是否可以在 QCombobox 的中心对齐文本。除了文本对齐之外,项目的字体似乎不受更改其 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_())

关于python - 如何在 QComboBox 中居中文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770287/

相关文章:

python - 在 linux 中运行 python 可执行文件

python - 使用 Dynatree 将节点从一棵树移动到另一棵树

crash - pyQt5 中退出时出现段错误,但 pyQt4 中没有

python - PyQt - 实现 QAbstractTableModel 以在 QTableView 中显示

c++ - QComboBox 放置位置正在移动

python - 如何将 QComboBox 中的文本存储在全局变量中

python - 获取 Django 中模型类的调用堆栈

python - 谷歌波 OnBlipSubscribed

python - 在 QGraphicsView 中显示 X 和 Y 坐标线

qt - 用户选择项目时如何在委托(delegate)中关闭 QComboBox 编辑器