简体   繁体   中英

How to center text in QComboBox?

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)

You can use setAlignment method:

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_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM