PyQt5中QComboBox文字和下拉项右对齐显示

 

 

from PyQt5.QtWidgets import QComboBox, QLineEdit, QListWidget, QCheckBox, QListWidgetItem
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
import sys

class MyQComboBox(QComboBox):
    def __init__(self, items):  # items==[str,str...]
        super().__init__()

        le = QLineEdit()        #显示框右对齐
        le.setAlignment(Qt.AlignRight)
        le.setReadOnly(True)
        super().setLineEdit(le)

        listWgt = QListWidget() #列表框右对齐
        for item in items:
            listWgtItem = QListWidgetItem(item)
            listWgtItem.setTextAlignment(Qt.AlignRight)
            listWgt.addItem(listWgtItem)
        super().setModel(listWgt.model())
        super().setView(listWgt)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    myCmb = MyQComboBox(['11','222','333','4444','55555'])
    myCmb.show()
    sys.exit(app.exec_())

参考链接:https://blog.csdn.net/qq_34305316/article/details/96488636 

 

 

  

posted @ 2021-03-08 14:24  云中虾  阅读(2089)  评论(0编辑  收藏  举报