1313from PyQt5 .QtGui import QBrush , QColor
1414from PyQt5 .QtGui import QPixmap
1515from PyQt5 .QtCore import pyqtSignal
16- from PyQt5 .QtWidgets import QVBoxLayout , QCompleter , QLineEdit
17- from PyQt5 .QtCore import QStringListModel , QSortFilterProxyModel , QRegExp
16+ from PyQt5 .QtWidgets import QCompleter
17+ from PyQt5 .QtCore import QStringListModel , QSortFilterProxyModel
1818# ui相关的小组件,非窗口
1919
20- class RoundQDialog (QDialog ):
21- closeEvent_ = QtCore .pyqtSignal ()
22- def __init__ (self , parent = None ):
23- # 可以随意拖动的圆角、阴影对话框
24- super (RoundQDialog , self ).__init__ (parent )
20+ BLUE_BUTTON_QSS = """
21+ QPushButton {
22+ background-color: #00A2E8;
23+ color: white;
24+ border: none;
25+ color:white;
26+ font-family: "Microsoft YaHei", "微软雅黑", "Segoe UI", Arial, sans-serif;
27+ font-size: 16pt;
28+ font-weight: bold;
29+ }
30+ """
31+
32+
33+ class RoundMixin :
34+ def _init_round (self ):
35+ # 可以随意拖动的圆角、阴影对话框的行为类
2536 self .border_width = 5
26- self .m_drag = False
37+ self ._dragging = False
2738 self .setAttribute (Qt .WA_TranslucentBackground )
2839 self .setWindowFlags (Qt .FramelessWindowHint | Qt .Window )
2940
3041 def paintEvent (self , event ):
3142 # # 阴影
32- path = QPainterPath ()
33- path .setFillRule (Qt .WindingFill )
43+ # path = QPainterPath()
44+ # path.setFillRule(Qt.WindingFill)
3445
35- pat = QPainter (self )
36- pat .setRenderHint (pat .Antialiasing )
37- pat .fillPath (path , QBrush (Qt .white ))
46+ p = QPainter (self )
47+ p .setRenderHint (p .Antialiasing )
48+ # p .fillPath(path, QBrush(Qt.white))
3849
3950 color = QColor (192 , 192 , 192 , 50 )
4051
@@ -44,110 +55,59 @@ def paintEvent(self, event):
4455 ref = QRectF (10 - i , 10 - i , self .width ()- (10 - i )* 2 , self .height ()- (10 - i )* 2 )
4556 # i_path.addRect(ref)
4657 i_path .addRoundedRect (ref , self .border_width , self .border_width )
47- color .setAlpha (150 - int ( i ** 0.5 * 50 )) # 为什么这个公式?
48- pat .setPen (color )
49- pat .drawPath (i_path )
58+ color .setAlpha (int ( 150 - i ** 0.5 * 50 ))
59+ p .setPen (color )
60+ p .drawPath (i_path )
5061
5162 # 圆角
52- pat2 = QPainter (self )
53- pat2 .setRenderHint (pat2 .Antialiasing ) # 抗锯齿
54- pat2 .setBrush (QtGui .QColor (242 , 242 , 242 , 255 ))
55- pat2 .setPen (Qt .transparent )
63+ p .setBrush (QtGui .QColor (242 , 242 , 242 , 255 ))
64+ p .setPen (Qt .transparent )
5665
5766 rect = self .rect ()
5867 rect .setLeft (9 )
5968 rect .setTop (9 )
6069 rect .setWidth (rect .width ()- 9 )
6170 rect .setHeight (rect .height ()- 9 )
62- pat2 .drawRoundedRect (rect , 10 , 10 )
71+ p .drawRoundedRect (rect , 10 , 10 )
6372
6473 def mousePressEvent (self , e ):
6574 if e .button () == Qt .LeftButton :
66- self .m_drag = True
67- self .m_DragPosition = e .globalPos () - self .pos ()
75+ self ._dragging = True
76+ self ._drag_offset = e .globalPos () - self .pos ()
6877 e .accept ()
69- # self.setCursor(QCursor(Qt.OpenHandCursor))
7078
7179 def mouseReleaseEvent (self , e ):
7280 if e .button () == Qt .LeftButton :
73- self .m_drag = False
74- # self.setCursor(QCursor(Qt.ArrowCursor))
81+ self ._dragging = False
7582
7683 def mouseMoveEvent (self , e ):
77- if Qt . LeftButton and self . m_drag :
78- self .move (e .globalPos () - self .m_DragPosition )
84+ if self . _dragging and e . buttons () & Qt . LeftButton :
85+ self .move (e .globalPos () - self ._drag_offset )
7986 e .accept ()
8087
81- def closeEvent (self , event ):
82- self .closeEvent_ .emit ()
8388
84- class RoundQWidget (QWidget ):
89+ class RoundQWidget (QWidget , RoundMixin ):
8590 barSetMineNum = QtCore .pyqtSignal (int )
8691 barSetMineNumCalPoss = QtCore .pyqtSignal ()
87- closeEvent_ = QtCore . pyqtSignal ()
92+ closeEvent_ = pyqtSignal ()
8893 def __init__ (self , parent = None ):
89- # 可以随意拖动的圆角、阴影对话框
90- super (RoundQWidget , self ).__init__ (parent )
91- self .border_width = 5
92- self .m_drag = False
93- self .setAttribute (Qt .WA_TranslucentBackground )
94- self .setWindowFlags (Qt .FramelessWindowHint | Qt .Window )
95-
96- def paintEvent (self , event ):
97- # # 阴影
98- path = QPainterPath ()
99- path .setFillRule (Qt .WindingFill )
100-
101- pat = QPainter (self )
102- pat .setRenderHint (pat .Antialiasing )
103- pat .fillPath (path , QBrush (Qt .white ))
104-
105- color = QColor (192 , 192 , 192 , 50 )
106-
107- for i in range (10 ):
108- i_path = QPainterPath ()
109- i_path .setFillRule (Qt .WindingFill )
110- ref = QRectF (10 - i , 10 - i , self .width ()- (10 - i )* 2 , self .height ()- (10 - i )* 2 )
111- # i_path.addRect(ref)
112- i_path .addRoundedRect (ref , self .border_width , self .border_width )
113- color .setAlpha (int (150 - i ** 0.5 * 50 ))
114- pat .setPen (color )
115- pat .drawPath (i_path )
116-
117- # 圆角
118- pat2 = QPainter (self )
119- pat2 .setRenderHint (pat2 .Antialiasing ) # 抗锯齿
120- pat2 .setBrush (QtGui .QColor (242 , 242 , 242 , 255 ))
121- pat2 .setPen (Qt .transparent )
122-
123- rect = self .rect ()
124- rect .setLeft (9 )
125- rect .setTop (9 )
126- rect .setWidth (rect .width ()- 9 )
127- rect .setHeight (rect .height ()- 9 )
128- pat2 .drawRoundedRect (rect , 10 , 10 )
129-
130- def mousePressEvent (self , e ):
131- if e .button () == Qt .LeftButton :
132- self .m_drag = True
133- self .m_DragPosition = e .globalPos () - self .pos ()
134- e .accept ()
135- # self.setCursor(QCursor(Qt.OpenHandCursor))
136-
137- def mouseReleaseEvent (self , e ):
138- if e .button () == Qt .LeftButton :
139- self .m_drag = False
140- # self.setCursor(QCursor(Qt.ArrowCursor))
141-
142- def mouseMoveEvent (self , e ):
143- if Qt .LeftButton and self .m_drag :
144- self .move (e .globalPos () - self .m_DragPosition )
145- e .accept ()
146-
94+ super ().__init__ (parent )
95+ self ._init_round ()
14796 def closeEvent (self , event ):
14897 self .closeEvent_ .emit ()
98+ event .accept ()
99+
149100
150101
102+ class RoundQDialog (QDialog , RoundMixin ):
103+ def __init__ (self , parent = None ):
104+ super ().__init__ (parent )
105+ self .setStyleSheet (BLUE_BUTTON_QSS )
106+ self ._init_round ()
107+
108+
109+
110+
151111class StatusLabel (QtWidgets .QLabel ):
152112 # 最上面的脸的控件,在这里重写一些方法
153113 leftRelease = QtCore .pyqtSignal () # 定义信号
0 commit comments