반응형

 

 

QComboBox 텍스트를 가운데 정렬하는 방법

 

ComboBox는 기본적으로 문자가 왼쪽 정렬되게 되어 있다.

Qt Designer 에서도 이것의 정렬하는 속성이 없다.

 

 

https://www.geeksforgeeks.org/pyqt5-how-to-make-text-center-align-for-non-editable-combobox/?ref=gcse 

 

PyQt5 - How to make text center align for non-editable ComboBox - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

위 링크 사이트의 내용을 참고로 하여 작성하였다.

 

# making it editable
self.comboBoxSerialBPS.setEditable(True)

SerialBPS_list = [ "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" ]
self.comboBoxSerialBPS.addItems(SerialBPS_list)
self.comboBoxSerialBPS.setCurrentIndex(7)

# getting the line edit of combo box
line_edit = self.comboBoxSerialBPS.lineEdit()

# setting line edit alignment to the center
line_edit.setAlignment(QtCore.Qt.AlignCenter)

font = QtGui.QFont('Arial', 12)
font.setBold(True)
line_edit.setFont(font)

# setting line edit to read only
line_edit.setReadOnly(True)

 

주의할 것은 ComboBox가 Editable 이어야 한다는 것이다.

 

https://doc.qt.io/qt-5/qcombobox.html#lineEdit

 

QComboBox Class | Qt Widgets 5.15.8

QComboBox Class The QComboBox widget is a combined button and popup list. More... Public Types enum InsertPolicy { NoInsert, InsertAtTop, InsertAtCurrent, InsertAtBottom, InsertAfterCurrent, …, InsertAlphabetically } enum SizeAdjustPolicy { AdjustToConte

doc.qt.io

 

이것을 모르고 designer에서 editable을 해제한 상태에서 했더니 setAlignment 에서 에러가 나왔었다.

이거 발견하는데 한 두시간... ㅠ.ㅠ

 

 

 

 

728x90
반응형

+ Recent posts