欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2020-10-19 10:18:41|栏目:Python代码|点击:

如下所示:

#!/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_())

上一篇:django自带的server 让外网主机访问方法

栏    目:Python代码

下一篇:python利用matplotlib库绘制饼图的方法示例

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

本文地址:http://www.codeinn.net/misctech/13515.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有