Skip to content

Commit fa48787

Browse files
committed
Merge branch 'master' of https://github.com/PyQt5/PyQt
2 parents 3212ba0 + d5d63a2 commit fa48787

15 files changed

+582
-7
lines changed

Demo/NewFramelessWindow.py

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
@description:
1010
"""
1111

12+
import sys
13+
1214
try:
1315
from PyQt5.QtCore import QEvent, QObject, QPoint, Qt, QTimer
1416
from PyQt5.QtGui import QColor, QMouseEvent, QPainter, QWindow
@@ -148,6 +150,18 @@ def __init__(self, *args, **kwargs):
148150
self.buttonClose.clicked.connect(self.close)
149151
self.setStyleSheet('#widgetTitleBar{background: rgb(232, 232, 232);}')
150152

153+
def showMinimized(self):
154+
flags = self.windowFlags()
155+
if sys.platform == 'darwin':
156+
# fix mac 最小化失效问题
157+
self.setWindowFlags((self.windowFlags() | Qt.CustomizeWindowHint) &
158+
(~Qt.WindowTitleHint))
159+
super(FramelessWindow, self).showMinimized()
160+
if sys.platform == 'darwin':
161+
# fix mac 最小化失效问题
162+
self.setWindowFlags(flags)
163+
self.show()
164+
151165
def changeEvent(self, event):
152166
"""窗口状态改变
153167
:param event:

QComboBox/CenterText.py

+53
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+
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

+83
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+
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

+12-1
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

QPainter/Data/qt-logo.png

2.63 KB
Loading

QPainter/Draw.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2022年12月12日
6+
@site: https://pyqt.site , https://github.com/PyQt5
7+
@description: QPainter画图
8+
"""
9+
import sys
10+
try:
11+
from PyQt5.QtWidgets import QApplication, QWidget, qApp
12+
from PyQt5.QtGui import QPainter, QFont, QColor, QPixmap
13+
from PyQt5.QtCore import Qt, pyqtSignal
14+
from PyQt5.Qt import QPoint, QPolygon
15+
except ImportError:
16+
from PySide2.QtWidgets import QApplication, QWidget, qApp
17+
from PySide2.QtGui import QPainter, QFont, QColor, QPixmap
18+
from PySide2.QtCore import Qt, pyqtSignal
19+
from PySide2.Qt import QPoint, QPolygon
20+
21+
22+
23+
class draw(QWidget):
24+
25+
26+
drawsig = pyqtSignal(bool)
27+
28+
def __init__(self):
29+
super(draw, self).__init__()
30+
self.setWindowTitle("QPainter画图")
31+
self._painter = QPainter()
32+
self.scale = 1.0
33+
self.pixmap = QPixmap()
34+
self.setMouseTracking(True)
35+
self.setFocusPolicy(Qt.WheelFocus)
36+
self.drawEnable = False
37+
self.points = []
38+
self.current_points = []
39+
self.drawsig.connect(self.setDrawEnable)
40+
41+
42+
def setDrawEnable(self, enable=False):
43+
self.drawEnable = enable
44+
self.update()
45+
46+
def mouseMoveEvent(self, ev):
47+
if self.drawEnable:
48+
def in_end_range(curr, first):
49+
return first.x() - 5 <= curr.x() <= first.x() + 5 and first.y() - 5 <= curr.y() <= first.y() + 5
50+
51+
if len(self.current_points) > 0 and in_end_range(ev.pos(), self.current_points[0]):
52+
self.current_points.append(self.current_points[0])
53+
self.points.append(self.current_points)
54+
self.current_points = []
55+
else:
56+
self.current_points.append(ev.pos())
57+
elif len(self.current_points) > 0:
58+
self.current_points.append(ev.pos())
59+
self.points.append(self.current_points)
60+
self.current_points = []
61+
62+
self.update()
63+
64+
def mousePressEvent(self, ev):
65+
if Qt.LeftButton & ev.button():
66+
self.drawsig.emit(True)
67+
def mouseReleaseEvent(self, ev):
68+
if Qt.LeftButton & ev.button():
69+
self.drawsig.emit(False)
70+
71+
def paintEvent(self, ev):
72+
if len(self.points) <= 0 and len(self.current_points) <= 0 : return
73+
p = self._painter
74+
p.begin(self)
75+
p.setRenderHint(QPainter.Antialiasing)
76+
p.setRenderHint(QPainter.HighQualityAntialiasing)
77+
p.setRenderHint(QPainter.SmoothPixmapTransform)
78+
p.scale(self.scale, self.scale)
79+
p.setPen(QColor(0, 0, 0))
80+
for pts in self.points:
81+
p.drawPolyline(QPolygon(pts))
82+
if len(self.current_points) > 0:
83+
p.drawPolyline(QPolygon(self.current_points))
84+
p.end()
85+
86+
87+
88+
if __name__ == '__main__':
89+
app = QApplication(sys.argv)
90+
mainWin = draw()
91+
mainWin.show()
92+
sys.exit(app.exec_())
93+

QPainter/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
# QPainter
1+
# QPainter
2+
3+
4+
- 目录
5+
- [利用QPainter绘制各种图形](#QPainter绘制各种图形)
6+
- [简易画板](#简易画板)
7+
8+
## 1、QPainter绘制各种图形
9+
[运行 StockDialog.py](StockDialog.py)
10+
11+
![CountDownClose](ScreenShot/StockDialog.gif)
12+
13+
## 2、简易画板
14+
[运行 Draw.py](Draw.py)
15+
16+
![CustomColorIcon](ScreenShot/Draw.gif)

QPainter/ScreenShot/Draw.gif

2.14 MB
Loading

QPainter/ScreenShot/StockDialog.gif

5.39 MB
Loading

0 commit comments

Comments
 (0)