PyQt5实现让QScrollArea支持鼠标拖动的操作方法

时间:2022-11-23 19:49:07

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/evn python3
# -*- coding: utf-8 -*-
 
import sys
from pyqt5.qtwidgets import (qapplication, qwidget, qscrollarea, qlabel)
from pyqt5.qtcore import qevent
 
 
class testwindow(qwidget):
    def __init__(self):
        super(testwindow, self).__init__()
        self.resize(800, 600)
        self.move(0, 0)
        # self.setmousetracking(false)
        self.last_time_move = 0
        self.initui()
 
    def initui(self):
        self.central_widget = qwidget(self)
 
        self.scroll_area = qscrollarea(self)
        self.scroll_area.setgeometry(0, 0, 400, 400)
        self.scroll_area.setwidgetresizable(true)
        self.scroll_bar = self.scroll_area.verticalscrollbar()
 
        self.scroll_contents = qwidget()
        self.scroll_contents.setgeometry(0, 0, 400, 800)
        self.scroll_contents.setminimumsize(380, 1000)
 
        self.label_1 = qlabel(self.scroll_contents)
        self.label_1.move(50, 100)
        self.label_1.settext("helloryan")
 
        self.label_2 = qlabel(self.scroll_contents)
        self.label_2.move(50, 200)
        self.label_2.settext("你好")
 
        self.label_3 = qlabel(self.scroll_contents)
        self.label_3.move(50, 300)
        self.label_3.settext("-----------")
 
        self.label_4 = qlabel(self.scroll_contents)
        self.label_4.move(50, 400)
        self.label_4.settext("542543255235432543252")
 
        self.label_5 = qlabel(self.scroll_contents)
        self.label_5.move(50, 500)
        self.label_5.settext("5432543262542")
 
        self.label_6 = qlabel(self.scroll_contents)
        self.label_6.move(50, 600)
        self.label_6.settext("4325432532")
 
        self.scroll_area.setwidget(self.scroll_contents)
        self.scroll_area.installeventfilter(self)
 
    # def mousemoveevent(self, event):
    #   if event.pos().x() > 0 and event.pos().x() < 400 and event.pos().y() > 0 and event.pos().y() < 400:
    #       if self.last_time_move == 0:
    #           self.last_time_move = event.pos().y()
    #
    #       distance = self.last_time_move - event.pos().y()
    #       self.scroll_bar.setvalue(self.scroll_bar.value() + distance)
    #
    #       self.last_time_move = event.pos().y()
    #       print("move%d" % event.pos().y())
    #
    # def mousereleaseevent(self, event):
    #   self.last_time_move = 0
    #   print("up")
 
    def eventfilter(self, source, event):
        if event.type() == qevent.mousemove:
            print(event.pos().y())
            
            if self.last_time_move == 0:
                self.last_time_move = event.pos().y()
                
            distance = self.last_time_move - event.pos().y()
            self.scroll_bar.setvalue(self.scroll_bar.value() + distance)
            self.last_time_move = event.pos().y()
            
        elif event.type() == qevent.mousebuttonrelease:
            self.last_time_move = 0
        return qwidget.eventfilter(self, source, event)
 
 
if __name__ == "__main__":
    app = qapplication(sys.argv)
    test = testwindow()
    test.show()
    sys.exit(app.exec_())

PyQt5实现让QScrollArea支持鼠标拖动的操作方法

以上这篇pyqt5实现让qscrollarea支持鼠标拖动的操作方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/SongAla/article/details/78732040