@@ -11,7 +11,7 @@ def __init__(self, *args, **kwargs):
11
11
super ().__init__ (* args , ** kwargs )
12
12
13
13
# 定义一个占位用的子控件
14
- self .widget = SiLabel (self )
14
+ self .attachment_ = SiLabel (self )
15
15
16
16
# 定义滚动条的框架
17
17
self .scroll_bar_frame_vertical = SiLabel (self )
@@ -34,7 +34,7 @@ def __init__(self, *args, **kwargs):
34
34
self .widget_scroll_animation .setBias (1 )
35
35
self .widget_scroll_animation .setCurrent ([0 , 0 ])
36
36
self .widget_scroll_animation .setTarget ([0 , 0 ])
37
- self .widget_scroll_animation .ticked .connect (lambda pos : self .widget .move (int (pos [0 ]), int (pos [1 ])))
37
+ self .widget_scroll_animation .ticked .connect (lambda pos : self .attachment_ .move (int (pos [0 ]), int (pos [1 ])))
38
38
39
39
def reloadStyleSheet (self ):
40
40
"""
@@ -45,36 +45,43 @@ def reloadStyleSheet(self):
45
45
self .scroll_bar_vertical .setStyleSheet (f"background-color: { SiGlobal .siui .colors ['SCROLL_BAR' ]} " )
46
46
self .scroll_bar_horizontal .setStyleSheet (f"background-color: { SiGlobal .siui .colors ['SCROLL_BAR' ]} " )
47
47
48
- def setWidget (self , widget ):
48
+ def setAttachment (self , widget ):
49
49
"""
50
50
绑定子控件,作为滚动的部件
51
51
:param widget:
52
52
:return:
53
53
"""
54
- self .widget .deleteLater ()
54
+ self .attachment_ .deleteLater ()
55
55
56
- self .widget = widget
57
- self .widget .setParent (self )
58
- self .widget .lower ()
56
+ self .attachment_ = widget
57
+ self .attachment_ .setParent (self )
58
+ self .attachment_ .lower ()
59
+
60
+ def attachment (self ):
61
+ """
62
+ 获取被绑定控件
63
+ :return: 被绑定控件
64
+ """
65
+ return self .attachment_
59
66
60
67
def _scroll_vertical_handler (self , pos ):
61
68
# 计算目标纵坐标
62
69
_ , y = pos
63
70
progress = y / (self .height () - self .scroll_bar_vertical .height ())
64
- target = - int (progress * (self .widget .height () - self .height ()))
71
+ target = - int (progress * (self .attachment_ .height () - self .height ()))
65
72
66
73
# 设置目标值并尝试启动
67
- self .widget_scroll_animation .setTarget ([self .widget .x (), target ])
74
+ self .widget_scroll_animation .setTarget ([self .attachment_ .x (), target ])
68
75
self .widget_scroll_animation .try_to_start ()
69
76
70
77
def _scroll_horizontal_handler (self , pos ):
71
78
# 计算目标横坐标
72
79
x , _ = pos
73
80
progress = x / (self .width () - self .scroll_bar_horizontal .width ())
74
- target = - int (progress * (self .widget .width () - self .width ()))
81
+ target = - int (progress * (self .attachment_ .width () - self .width ()))
75
82
76
83
# 设置目标值并尝试启动
77
- self .widget_scroll_animation .setTarget ([target , self .widget .y ()])
84
+ self .widget_scroll_animation .setTarget ([target , self .attachment_ .y ()])
78
85
self .widget_scroll_animation .try_to_start ()
79
86
80
87
def resizeEvent (self , event ):
@@ -87,20 +94,20 @@ def resizeEvent(self, event):
87
94
88
95
# 判断长宽是否超过自身的长宽,从而确定是否显示滚动条
89
96
# 水平
90
- if self .widget .width () <= self .width ():
97
+ if self .attachment_ .width () <= self .width ():
91
98
self .scroll_bar_horizontal .setVisible (False )
92
99
else :
93
100
self .scroll_bar_horizontal .setVisible (True )
94
101
95
102
# 竖直
96
- if self .widget .height () <= self .height ():
103
+ if self .attachment_ .height () <= self .height ():
97
104
self .scroll_bar_vertical .setVisible (False )
98
105
else :
99
106
self .scroll_bar_vertical .setVisible (True )
100
107
101
108
# 根据滚动区域的大小和子控件的大小,计算出滚动条的长度或宽度
102
- self .scroll_bar_horizontal .resize (self .width () * self .width () // self .widget .width (), 8 )
103
- self .scroll_bar_vertical .resize (8 , self .height () * self .height () // self .widget .height ())
109
+ self .scroll_bar_horizontal .resize (self .width () * self .width () // self .attachment_ .width (), 8 )
110
+ self .scroll_bar_vertical .resize (8 , self .height () * self .height () // self .attachment_ .height ())
104
111
105
112
# 设置拖动限制
106
113
self .scroll_bar_horizontal .setMoveLimits (0 , 0 , self .width (), 8 )
@@ -118,11 +125,11 @@ def wheelEvent(self, event):
118
125
119
126
# 根据滚动方向,目标值加或减滚动强度,并更新目标值
120
127
target [1 ] += strength if event .angleDelta ().y () > 0 else - strength
121
- target [1 ] = min (0 , max (self .height () - self .widget .height (), target [1 ])) # 防止滚出限制范围
128
+ target [1 ] = min (0 , max (self .height () - self .attachment_ .height (), target [1 ])) # 防止滚出限制范围
122
129
self .widget_scroll_animation .setTarget (target )
123
130
124
131
# 计算滚动条的目标位置
125
- process = - target [1 ] / (self .widget .height () - self .height ())
132
+ process = - target [1 ] / (self .attachment_ .height () - self .height ())
126
133
scroll_bar_vertical_target_y = int (process * (self .height () - self .scroll_bar_vertical .height ()))
127
134
128
135
# 如果竖直方向滚动条可见,尝试启动动画
0 commit comments