Skip to content

Commit 3372f59

Browse files
committed
QComboBox 文本居中显示
1 parent 22f298b commit 3372f59

File tree

6 files changed

+151
-3
lines changed

6 files changed

+151
-3
lines changed

QComboBox/CenterText.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on 2022/09/03
5+
@author: Irony
6+
@site: https://pyqt.site https://github.com/PyQt5
7+
@email: 892768447@qq.com
8+
@file: CenterText.py
9+
@description: 文字居中对齐
10+
"""
11+
12+
import sys
13+
14+
try:
15+
from PyQt5.QtCore import Qt
16+
from PyQt5.QtWidgets import QApplication, QStyle, QVBoxLayout, QWidget
17+
except ImportError:
18+
from PySide2.QtCore import Qt
19+
from PySide2.QtWidgets import QApplication, QStyle, QVBoxLayout, QWidget
20+
21+
from Lib.CtComboBox import CtComboBox
22+
23+
24+
class Window(QWidget):
25+
26+
def __init__(self, *args, **kwargs):
27+
super(Window, self).__init__(*args, **kwargs)
28+
layout = QVBoxLayout(self)
29+
30+
c1 = CtComboBox(self)
31+
c1.addItems(['item-%s' % i for i in range(10)])
32+
layout.addWidget(c1)
33+
34+
# 可编辑
35+
c2 = CtComboBox(self)
36+
c2.setEditable(True)
37+
c2.lineEdit().setAlignment(Qt.AlignCenter)
38+
c2.addItems(['item-%s' % i for i in range(10)])
39+
layout.addWidget(c2)
40+
41+
# 带图标
42+
c3 = CtComboBox(self)
43+
for i in range(10):
44+
c3.addItem(c3.style().standardIcon(QStyle.SP_ComputerIcon),
45+
'item-%s' % i)
46+
layout.addWidget(c3)
47+
48+
49+
if __name__ == '__main__':
50+
app = QApplication(sys.argv)
51+
w = Window()
52+
w.show()
53+
sys.exit(app.exec_())

QComboBox/Lib/CtComboBox.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on 2022/09/04
5+
@author: Irony
6+
@site: https://pyqt.site https://github.com/PyQt5
7+
@email: 892768447@qq.com
8+
@file: CtComboBox.py
9+
@description: 文字居中对齐
10+
"""
11+
12+
try:
13+
from PyQt5.QtCore import QRect, Qt
14+
from PyQt5.QtGui import QIcon, QPalette
15+
from PyQt5.QtWidgets import QComboBox, QProxyStyle
16+
except ImportError:
17+
from PySide2.QtCore import QRect, Qt
18+
from PySide2.QtGui import QIcon, QPalette
19+
from PySide2.QtWidgets import QComboBox, QProxyStyle
20+
21+
22+
class ComboBoxStyle(QProxyStyle):
23+
24+
def drawControl(self, element, option, painter, widget=None):
25+
if element == QProxyStyle.CE_ComboBoxLabel:
26+
# https://github.com/qt/qtbase/blob/5.15.2/src/widgets/styles/qcommonstyle.cpp#L2200
27+
editRect = self.subControlRect(QProxyStyle.CC_ComboBox, option,
28+
QProxyStyle.SC_ComboBoxEditField,
29+
widget)
30+
painter.save()
31+
painter.setClipRect(editRect)
32+
if not option.currentIcon.isNull():
33+
# 绘制图标
34+
mode = QIcon.Normal if (
35+
option.state &
36+
QProxyStyle.State_Enabled) else QIcon.Disabled
37+
pixmap = option.currentIcon.pixmap(
38+
widget.window().windowHandle() if widget else None,
39+
option.iconSize, mode)
40+
iconRect = QRect(editRect)
41+
iconRect.setWidth(option.iconSize.width() + 4)
42+
iconRect = self.alignedRect(option.direction,
43+
Qt.AlignLeft | Qt.AlignVCenter,
44+
iconRect.size(), editRect)
45+
if option.editable:
46+
painter.fillRect(iconRect,
47+
option.palette.brush(QPalette.Base))
48+
self.drawItemPixmap(painter, iconRect, Qt.AlignCenter, pixmap)
49+
50+
if option.direction == Qt.RightToLeft:
51+
editRect.translate(-4 - option.iconSize.width(), 0)
52+
else:
53+
editRect.translate(option.iconSize.width() + 4, 0)
54+
if option.currentText and not option.editable:
55+
# 考虑右边箭头位置
56+
arrowRect = self.subControlRect(QProxyStyle.CC_ComboBox, option,
57+
QProxyStyle.SC_ComboBoxArrow,
58+
widget)
59+
editRect.setWidth(editRect.width() + arrowRect.width())
60+
# 绘制居中文字
61+
self.drawItemText(
62+
painter, editRect.adjusted(1, 0, -1, 0),
63+
self.visualAlignment(option.direction, Qt.AlignCenter),
64+
option.palette, option.state & QProxyStyle.State_Enabled,
65+
option.currentText)
66+
painter.restore()
67+
return
68+
super(ComboBoxStyle, self).drawControl(element, option, painter, widget)
69+
70+
71+
class CtComboBox(QComboBox):
72+
73+
def __init__(self, *args, **kwargs):
74+
super(CtComboBox, self).__init__(*args, **kwargs)
75+
# 绑定每个元素添加信号,用于设置文本居中
76+
self.model().rowsInserted.connect(self._onRowsInserted)
77+
self.setStyle(ComboBoxStyle())
78+
79+
def _onRowsInserted(self, index, first, last):
80+
if first < 0:
81+
return
82+
for i in range(first, last + 1):
83+
self.view().model().item(i).setTextAlignment(Qt.AlignCenter)

QComboBox/Lib/__init__.py

Whitespace-only changes.

QComboBox/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
- 目录
44
- [下拉数据关联](#1下拉数据关联)
5+
- [文本居中显示](#2文本居中显示)
56

67
## 1、下拉数据关联
8+
79
[运行 CityLinkage.py](CityLinkage.py)
810

911
一个省市区关联的三级联动,数据源在data.json中
@@ -12,4 +14,13 @@
1214
2. 并根据唯一编码过滤,为了不影响内容显示,唯一编码的角色为`ToolTipRole`
1315
3.`QColumnView`可以实现类似效果
1416

15-
![CityLinkage](ScreenShot/CityLinkage.gif)
17+
![CityLinkage](ScreenShot/CityLinkage.gif)
18+
19+
## 2、文本居中显示
20+
21+
[运行 CenterText.py](CenterText.py)
22+
23+
1. 使用`QProxyStyle`对文件居中显示
24+
2. 新增得item数据使用`setTextAlignment`对齐
25+
26+
![CenterText](ScreenShot/CenterText.png)

QComboBox/ScreenShot/CenterText.png

4.89 KB
Loading

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Badge](https://img.shields.io/badge/link-996.icu-%23FF4D5B.svg?style=flat-square)](https://996.icu/#/zh_CN)
66
[![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg?style=flat-square)](https://github.com/996icu/996.ICU/blob/master/LICENSE)
77

8-
https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分享大家平时学习中记录的笔记和例子,以及对遇到的问题进行收集整理。
8+
[https://pyqt.site](https://pyqt.site) 论坛是专门针对PyQt5学习和提升开设的网站,分享大家平时学习中记录的笔记和例子,以及对遇到的问题进行收集整理。
99

1010
[![GitHub watchers](https://img.shields.io/github/watchers/PyQt5/PyQt.svg?style=social&label=Watch)](https://github.com/PyQt5/PyQt)
1111
[![GitHub stars](https://img.shields.io/github/stars/PyQt5/PyQt.svg?style=social)](https://github.com/PyQt5/PyQt)
@@ -17,7 +17,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
1717

1818
[自定义控件](https://github.com/PyQt5/CustomWidgets)
1919

20-
# QQ群
20+
## QQ群
2121

2222
[PyQt 学习](https://jq.qq.com/?_wv=1027&k=5QVVEdF)
2323

@@ -97,6 +97,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
9797
- Input Widgets
9898
- [QComboBox](QComboBox)
9999
- [下拉数据关联](QComboBox/CityLinkage.py)
100+
- [文本居中显示](QComboBox/CenterText.py)
100101
- [QFontComboBox](QFontComboBox)
101102
- [QLineEdit](QLineEdit)
102103
- [QTextEdit](QTextEdit)

0 commit comments

Comments
 (0)