源码
蓝奏云:PySide6实战汇总
密码:adro
从上一章开始看
本章我们需要正在上一章的基础上来做调整,所以还没有看过上一章的朋友可以回头看看
改变后的ui
在本章我们给ui加上了退出程序的按钮
并且做了简单的布局,拉伸功能必须对控件进行布局
![](https://img-blog.csdnimg.cn/img_convert/a85647841c53b5bf3de7e9150a5cab57.png)
布局
各控件的属性设置,请自行在源码ui文件中查看
![](https://img-blog.csdnimg.cn/img_convert/2167a5fe5c49b061cc8a11932a452aa8.png)
代码
上一章已经讲过怎么把ui文件转化为py文件了,所以以后也不再说明
import sys
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from ui import ui_main_win
# 主窗口
class main_win(QWidget):
def __init__(self,parent = None):
# 从文件中加载UI定义
super(main_win, self).__init__(parent)
self.ui = ui_main_win.Ui_Form()
self.ui.setupUi(self)
self.setWindowFlag(Qt.FramelessWindowHint) #将界面设置为无框
self.setAttribute(Qt.WA_TranslucentBackground) #将界面属性设置为半透明
self.shadow = QGraphicsDropShadowEffect() #设定一个阴影,半径为 4,颜色为 2, 10, 25,偏移为 0,0
self.shadow.setBlurRadius(4)
self.shadow.setColor(QColor(2, 10, 25))
self.shadow.setOffset(0, 0)
self.ui.frame.setGraphicsEffect(self.shadow) #为frame设定阴影效果
# 绑定退出按钮信号
self.ui.pushButton_2.clicked.connect(app.quit)
# 开启鼠标跟踪后,鼠标离开窗口或进入窗口会触发 mouseMoveEvent 事件
self.setMouseTracking(True)
# 初始化各扳机的状态
self.initDrag()
# 主窗口绑定事件过滤器
self.ui.frame.installEventFilter(self) # 初始化事件过滤器
# -------------------------------------------------事件过滤器-------------------------------------------------
def eventFilter(self, obj, event):
# 事件过滤器,用于解决鼠标进入其它控件后还原为标准鼠标样式
if isinstance(event, QEnterEvent):
self.setCursor(Qt.ArrowCursor)
return super().eventFilter(obj, event)
# -----------------------------------------------移动与拉伸功能------------------------------------------------
# -----------------------------------------------移动与拉伸功能------------------------------------------------
# 初始化各扳机的状态
def initDrag(self):
self._move_drag = False
self._corner_drag = False
self._bottom_drag = False
self._right_drag = False
# 鼠标按下所执行的功能
def mousePressEvent(self, event):
# globalPosition为鼠标位置 , pos,position 为窗口的位置 , cursor_win_pos 为鼠标在窗口中的位置
if event.button() == Qt.LeftButton:
self.cursor_win_pos = event.globalPosition() - self.pos()
# 移动事件
if self.cursor_win_pos.x() < self.ui.frame.size().width() and self.cursor_win_pos.y() < self.ui.frame.size().height():
self._move_drag = True
event.accept()
# 右下角边界拉伸事件
elif self.cursor_win_pos.x() > self.ui.frame.size().width() and self.cursor_win_pos.y() > self.ui.frame.size().height():
self._corner_drag = True
event.accept()
# 下边界拉伸事件
elif self.cursor_win_pos.x() < self.ui.frame.size().width() and self.cursor_win_pos.y() > self.ui.frame.size().height():
self._bottom_drag = True
event.accept()
# 右边界拉伸事件
elif self.cursor_win_pos.x() > self.ui.frame.size().width() and self.cursor_win_pos.y() < self.ui.frame.size().height():
self._right_drag = True
event.accept()
# 鼠标移动所执行的功能
def mouseMoveEvent(self, event):
# 移动事件
if Qt.LeftButton and self._move_drag:
m_Point = event.globalPosition() - self.cursor_win_pos
self.move(m_Point.x() ,m_Point.y())
event.accept()
# 右下角边界拉伸事件
elif Qt.LeftButton and self._corner_drag:
self.resize(event.position().x()+10 , event.position().y()+10)
event.accept()
# 下边界拉伸事件
elif Qt.LeftButton and self._bottom_drag:
self.resize(self.width() , event.position().y()+10)
event.accept()
# 右边界拉伸事件
elif Qt.LeftButton and self._right_drag:
self.resize(event.position().x()+10 , self.height())
event.accept()
# 获取鼠标在窗口中的位置来改变鼠标的图标
# 右下角边界光标事件
self.cursor_win_pos = event.globalPosition() - self.pos()
if self.cursor_win_pos.x() > self.ui.frame.size().width() and self.cursor_win_pos.y() > self.ui.frame.size().height():
self.setCursor(Qt.SizeFDiagCursor)
# 下边界光标事件
elif self.cursor_win_pos.x() < self.ui.frame.size().width() and self.cursor_win_pos.y() > self.ui.frame.size().height():
self.setCursor(Qt.SizeVerCursor)
# 右边界光标事件
elif self.cursor_win_pos.x() > self.ui.frame.size().width() and self.cursor_win_pos.y() < self.ui.frame.size().height():
self.setCursor(Qt.SizeHorCursor)
# 正常光标事件
else:
self.setCursor(Qt.ArrowCursor)
# 鼠标弹起后,恢复各扳机的状态
def mouseReleaseEvent(self, event):
self._move_drag = False
self._corner_drag = False
self._bottom_drag = False
self._right_drag = False
if __name__ == '__main__':
# 每一个 PySide6 应用都必须创建一个应用对象
app = QApplication([])
main_win = main_win()
main_win.show()
sys.exit(app.exec())
效果
![](https://img-blog.csdnimg.cn/img_convert/30c154bcdc2dd5e29f09248256b728b6.gif)
下期预告
下一期为:设置系统托盘