pyqt5 QScrollArea设置在自定义侧(任何位置)
作者:yunyuyuan 时间:2023-05-22 09:33:51
本例设置为垂直左侧scroll
主要思想是利用一个长度为0的mid_frame,高度为待设置qwidget的高度,用mid_frame的moveEvent事件驱动qwidget的move
我项目的效果图:
代码及注释
from PyQt5.Qt import *
from sys import argv
# 主窗口
class Main(QMainWindow):
def __init__(self):
super().__init__(None)
self.setGeometry(500, 500, 500, 500)
# 实例化
self.main = MainFrame(self)
self.scroll = ScrollFrame(self, self.main, 40)
self.show()
def resizeEvent(self, e):
# resize改变scroll窗口的高度使其与自己相同
self.scroll.resize(self.scroll.width(), self.height())
#需要配备scroll的窗口
class MainFrame(QFrame):
def __init__(self, father):
super().__init__(father)
self.father = father
self.setGeometry(50, 50, 100, 1500)
# 测试按钮
for i in range(15):
b = QPushButton(str(i), self)
b.setGeometry(0, i*100, 100, 100)
# 自定义滚轮事件
def wheelEvent(self, e):
if e.angleDelta().y() > 0:
self.move(self.x(), self.y() + 60)
else:
self.move(self.x(), self.y() - 60)
# 改变scroll的值
self.father.scroll.bar.setValue(abs(self.y()))
def resizeEvent(self, e):
# resize改变mid_frame的高度使其与自己相同
self.father.scroll.mid_frame.setGeometry(0, 0, 0, self.height())
# 承载scrollarea的窗口
class ScrollFrame(QFrame):
def __init__(self, father, parent, pos_x):
super().__init__(father)
self.parent_, self.father, self.pox_x = parent, father, pos_x
self.mid_frame = MidFrame(self)
self.mid_frame.setGeometry(0, 0, 0, self.parent_.height())
self.scroll = QScrollArea()
# 实例化verticalbar以供改变scroll的值
self.bar = self.scroll.verticalScrollBar()
# 绑定中间窗口
self.scroll.setWidget(self.mid_frame)
# 自动隐藏和出现
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
# 加入到承载frame
layout = QGridLayout(self)
layout.addWidget(self.scroll, 0, 0)
self.setLayout(layout)
# 设置承载fram的size和scrollarea一样
self.setGeometry(pos_x, 0, 20, self.father.height())
def resizeEvent(self, e):
# resize改变scroll的s高度使其与自己一样
self.scroll.setGeometry(0, 0, 20, self.height())
# 接受scroll事件的中间窗口
class MidFrame(QFrame):
def __init__(self, father):
super().__init__(father)
self.father = father
def moveEvent(self, e):
# move事件绑定实际滚动窗口的move
self.father.parent_.move(self.father.parent_.x(), e.pos().y())
app = QApplication(argv)
main = Main()
app.exec_()
本例效果:
来源:https://segmentfault.com/a/1190000020478123
标签:pyqt5,QScrollArea,自定义侧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python深度学习TensorFlow神经网络基础概括
2022-08-13 02:57:18
![](https://img.aspxhome.com/file/2023/0/127600_0s.png)
Python 使用csv库处理CSV文件的方法
2023-03-16 21:37:24
![](https://img.aspxhome.com/file/2023/9/65529_0s.png)
木鸟:ASP缓存类无错版
2008-02-20 12:53:00
Python matplotlib绘制散点图配置(万能模板案例)
2023-08-02 23:04:18
![](https://img.aspxhome.com/file/2023/9/61989_0s.png)
mysql 按中文字段排序
2024-01-14 18:04:19
pandas如何删除没有列名的列浅析
2021-05-17 11:08:47
![](https://img.aspxhome.com/file/2023/4/118064_0s.png)
OpenCV 表盘指针自动读数的示例代码
2023-12-29 05:46:38
![](https://img.aspxhome.com/file/2023/7/68917_0s.jpg)
python中使用while循环的实例
2022-12-25 00:23:57
![](https://img.aspxhome.com/file/2023/9/101589_0s.jpg)
详解python中Numpy的属性与创建矩阵
2023-08-29 03:35:17
php mysql获取表字段名称和字段信息的三种方法
2023-11-18 22:47:26
![](https://img.aspxhome.com/file/2023/9/104179_0s.png)
Pandas中DataFrame数据删除详情
2023-12-18 10:40:02
python对于requests的封装方法详解
2023-02-06 15:29:48
JS异步函数队列功能实例分析
2024-04-22 13:26:28
ubuntu系统下使用pm2设置nodejs开机自启动的方法
2023-10-02 22:23:16
mybatis plus generator 根据数据库自动生成实体类的实现示例
2024-01-25 11:48:00
![](https://img.aspxhome.com/file/2023/3/130903_0s.png)
Python深度学习线性代数示例详解
2022-06-03 23:52:14
![](https://img.aspxhome.com/file/2023/8/91828_0s.png)
SQL的SUBSTR()函数使用介绍
2024-01-16 14:19:27
Python分析特征数据类别与预处理方法速学
2023-04-29 09:55:52
![](https://img.aspxhome.com/file/2023/6/103116_0s.jpg)
使用python实现简单五子棋游戏
2022-09-22 04:16:35
![](https://img.aspxhome.com/file/2023/4/133844_0s.png)
MYSQL教程:查询优化之调度和锁定
2009-02-27 15:58:00