欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-14 15:31:28
如下所示: #!/usr/bin/evn python3 # -*- coding: utf-8 -*- import sys from pyqt5....

如下所示:

#!/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支持鼠标拖动的操作方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: