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

yipeiwu_com5年前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_())

以上这篇PyQt5实现让QScrollArea支持鼠标拖动的操作方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python调用C# Com dll组件实战教程

Python调用C# Com dll组件实战教程

之前公司有套C# AES加解密方案,但是方案加密用的是Rijndael类,而非AES的四种模式(ECB、CBC、CFB、OFB,这四种用的是RijndaelManaged类),Pytho...

python3安装speech语音模块的方法

python3安装speech语音模块的方法

在windows平台上使用pyhton编写语音识别程序需要用到speech模块,speech模块支持的主要功能有:文本合成语音,将键盘输入的文本信息转换为语音信号方式输出;语音识别,将输...

python 输入一个数n,求n个数求乘或求和的实例

求和 try: while True: n=input() s=1 for x in raw_input().split(): s=s+int(x) prin...

python 实现检验33品种数据是否是正态分布

我就废话不多说了,直接上代码吧! # -*- coding: utf-8 -*- """ Created on Thu Jun 22 17:03:16 2017 @author: y...

PyQt4编程之让状态栏显示信息的方法

赶快记录一下,只是懂皮毛,或许多积累就好了 import sys from PyQt4 import QtGui class MainWindow(QtGui.QMainWindo...