news 2026/5/5 0:24:26

PySide6 自定义侧边栏 实现思路与代码详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
PySide6 自定义侧边栏 实现思路与代码详解

PySide6 自定义侧边栏 实现思路与代码详解

PySide6虽然得益于Qt框架的强大与Python语法的快速开发,但是默认提供的主题不符合现代UI的省美!比如:侧边栏一般也叫导航栏(更多是手机平板的等设备)。

写在前边

笔者使用的是LinuxGnome桌面系统,其他显示效果请自行尝试!

为了更方便的描述,这里分为如下俩中情况:

  1. 默认展开 => 切换后先收缩
  2. 默认收缩 => 切换后先放大

效果演示


默认展开

注意:

  • 紫色为:QFrame嵌套QVboxLayout是主窗口
  • 绿色为:QFrame嵌套QVboxLayout是侧边栏(父类为主窗口的QFrame)
实现思路

显示与隐藏核心:

  • 默认展开:QFrame设置最大与最小宽度为155
  • 切换:重新设置QFrame的最小宽度

温馨提示:
setFixedWidth<=>setMinimumWidth+setMaximumWidth
选择QFrame一是参考Qt Designer,二是继承QWidget更好的设置 属性

核心代码解读
deftoggle_sidebar(self):"""show or hide toggle button text"""self.anim=QPropertyAnimation(self.__sidebar_frame,b"minimumWidth")self.anim.stop()ifself.__sidebar_visible==False:# hide: right to leftself.anim.setStartValue(self.__sidebar_frame.width())self.anim.setEndValue(40)else:# show: left to rightself.anim.setStartValue(self.__sidebar_frame.width())self.anim.setEndValue(155)self.__sidebar_frame.setFixedWidth(40)self.anim.setEasingCurve(QEasingCurve.Type.InOutQuad)self.anim.setDuration(500)self.anim.start()self.__sidebar_visible=notself.__sidebar_visible

重点如下:

  1. QPropertyAnimation:实现显示与隐藏的动画效果;官方文档
  2. QEasingCurve:使得动画更丝滑;官方文档
  3. setDuration:设置动画的持续时间;官方文档

⚠️注意

  1. 取消self.__sidebar_frame.setFixedWidth(40)将无法正常收缩
  2. 改变整体布局为QHBoxLayout,展开与收缩就会从QFrame中心垂线收缩展开

完整代码:

#hide_sidebar.pyimportsysfromPySide6.QtWidgetsimport(QWidget,QFrame,QVBoxLayout,QPushButton,QApplication)fromPySide6.QtCoreimportQPropertyAnimation,QEasingCurveclassQCustomeWidget(QWidget):"""Custome sidebar """def__init__(self):self.__sidebar_visible=Falsesuper().__init__()self.__setup_ui()self.__setup_event_handel()def__setup_ui(self):# widget sizeself.setFixedSize(400,300)# global layout use framelayout then vboxlayoutself.__global_frame=QFrame(self)self.__global_frame.setContentsMargins(0,0,0,0)self.__global_layout=QVBoxLayout(self.__global_frame)self.__global_layout.setSpacing(0)self.__global_layout.setContentsMargins(0,0,0,0)# sidebar frame framelayoutself.__sidebar_frame=QFrame(self.__global_frame)self.__sidebar_layout=QVBoxLayout(self.__sidebar_frame)self.__sidebar_frame.setLayout(self.__sidebar_layout)self.__sidebar_frame.setContentsMargins(0,0,0,0)self.__sidebar_layout.setContentsMargins(0,0,0,0)self.__sidebar_layout.setSpacing(0)# form sizeself.__sidebar_frame.setFixedSize(155,self.height())self.__sidebar_frame.setStyleSheet("background-color:grey;")# toggle and home buttonself.__toggle_btn=QPushButton('show: button text',self.__sidebar_frame)self.__toggle_btn.setStyleSheet(".QPushButton {background-color: rgb(56, 56, 60);text-align: left;}")self.__home_btn=QPushButton('home: button text',self.__sidebar_frame)self.__home_btn.setStyleSheet(".QPushButton {background-color: rgb(56, 56, 60);text-align: left;}")# add buttonself.__sidebar_layout.addWidget(self.__toggle_btn)self.__sidebar_layout.addWidget(self.__home_btn)# add Widgetself.__global_layout.addWidget(self.__sidebar_frame)def__setup_event_handel(self):self.__toggle_btn.clicked.connect(self.toggle_sidebar)deftoggle_sidebar(self):"""show or hide toggle button text"""self.anim=QPropertyAnimation(self.__sidebar_frame,b"minimumWidth")self.anim.stop()ifself.__sidebar_visible==False:# hide: right to leftself.anim.setStartValue(self.__sidebar_frame.width())self.anim.setEndValue(40)else:# show: left to rightself.anim.setStartValue(self.__sidebar_frame.width())self.anim.setEndValue(155)self.__sidebar_frame.setFixedWidth(40)self.anim.setEasingCurve(QEasingCurve.Type.OutQuad)self.anim.setDuration(500)self.anim.start()self.__sidebar_visible=notself.__sidebar_visibledefmain():app=QApplication([])sidebar=QCustomeWidget()sidebar.show()sys.exit(app.exec())if__name__=="__main__":main()

默认隐藏

注意:

  • 紫色为:QFrame嵌套QVboxLayoutQHboxLayout是主窗口
  • 绿色为:QFrame嵌套QVboxLayout是侧边栏(父类为主窗口的QFrame)
实现思路

与默认展开不同的是:

  • 右侧需要控件用于占用剩余的空间.
完整代码
#show_sidebar.pyimportsysfromPySide6.QtWidgetsimport(QWidget,QFrame,QHBoxLayout,QVBoxLayout,QLabel,QPushButton,QApplication)fromPySide6.QtCoreimport(QPropertyAnimation,QEasingCurve)classQCustomeSideBar(QWidget):"""Custome sidebar """def__init__(self):self.__sidebar_visible=Falsesuper().__init__()self.__setup_ui()self.__setup_event_handel()def__setup_ui(self):# widget sizeself.setFixedSize(400,300)# global layout use framelayout then vboxlayoutself.__global_frame=QFrame(self)self.__global_frame.setContentsMargins(0,0,0,0)self.__global_layout=QVBoxLayout(self.__global_frame)# or use# self.__global_layout = QHBoxLayout(self.__global_frame)self.__global_layout.setSpacing(0)self.__global_layout.setContentsMargins(0,0,0,0)# content and sidebar frame framelayoutself.__sidebar_frame=QFrame(self.__global_frame)self.__sidebar_layout=QVBoxLayout(self.__sidebar_frame)self.__sidebar_frame.setLayout(self.__sidebar_layout)self.__sidebar_frame.setContentsMargins(0,0,0,0)self.__sidebar_layout.setContentsMargins(0,0,0,0)self.__sidebar_layout.setSpacing(0)# form sizeself.__sidebar_frame.setFixedSize(40,self.height())self.__sidebar_frame.setStyleSheet("background-color:grey;")# toggle and home buttonself.__toggle_btn=QPushButton('show: button text',self.__sidebar_frame)self.__toggle_btn.setStyleSheet(".QPushButton {background-color: rgb(56, 56, 60);text-align: left;}")self.__home_btn=QPushButton('home: button text',self.__sidebar_frame)self.__home_btn.setStyleSheet(".QPushButton {background-color: rgb(56, 56, 60);text-align: left;}")# add buttonself.__sidebar_layout.addWidget(self.__toggle_btn)self.__sidebar_layout.addWidget(self.__home_btn)# add Widgetself.__global_layout.addWidget(self.__sidebar_frame)self.__content_button=QLabel("text")self.__content_button.setStyleSheet("text-align: right; margin-left:115px;")self.__global_layout.addWidget(self.__content_button)def__setup_event_handel(self):self.__toggle_btn.clicked.connect(self.toggle_sidebar)deftoggle_sidebar(self):"""show or hide toggle button text"""self.anim=QPropertyAnimation(self.__sidebar_frame,b"minimumWidth",self.__global_frame)self.anim.stop()ifself.__sidebar_visible==False:# show: left to rightself.anim.setStartValue(self.__sidebar_frame.width())self.anim.setEndValue(155)else:# hide: right to leftself.anim.setStartValue(self.__sidebar_frame.width())self.anim.setEndValue(40)self.anim.setEasingCurve(QEasingCurve.Type.InOutQuad)self.anim.setDuration(500)self.anim.start()self.__sidebar_visible=notself.__sidebar_visibledefmain():app=QApplication([])sidebar=QCustomeSideBar()sidebar.show()sys.exit(app.exec())if__name__=="__main__":main()
一起学习与探讨

点击链接加入群聊【PySide6学习交流群】:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/4 10:58:36

基于PSCAD EMTDC软件的风力发电机组控制系统仿真设计与验证

风力发电机控制系统仿真设计 风力发电系统动态模拟仿真 光伏发电系统 本设计主要依据风力发电机组的控制目标和控制策略&#xff0c;通过使用电力系统动态模拟仿真软件PSCAD/EMTDC&#xff0c;建立变桨距风力发电机组控制系统的模型。 为了验证控制系统模型的可用性&#xff0c…

作者头像 李华
网站建设 2026/4/30 23:36:09

基于PHP的画稿定制系统的设计与实现源码设计与文档

前言 基于 PHP 的画稿定制系统&#xff0c;直击 “用户需求表达模糊、画师资源分散、定制流程无保障” 的核心痛点&#xff0c;依托 PHP 的高效后端处理能力与 Laravel 框架的快速开发优势&#xff0c;构建 “需求匹配 创作协同 安全交易” 的一体化画稿定制服务平台。传统模…

作者头像 李华
网站建设 2026/5/2 12:48:45

Chat UI Kit React:30分钟搭建专业级聊天界面的终极指南

Chat UI Kit React&#xff1a;30分钟搭建专业级聊天界面的终极指南 【免费下载链接】chat-ui-kit-react Build your own chat UI with React components in few minutes. Chat UI Kit from chatscope is an open source UI toolkit for developing web chat applications. 项…

作者头像 李华
网站建设 2026/5/1 0:19:08

vfox插件管理完全指南:轻松掌握多版本工具切换技巧

vfox插件管理完全指南&#xff1a;轻松掌握多版本工具切换技巧 【免费下载链接】vfox 项目地址: https://gitcode.com/gh_mirrors/vf/vfox Version-Fox&#xff08;简称vfox&#xff09;是一款功能强大的跨平台版本管理器&#xff0c;专门解决开发者在不同项目间切换环…

作者头像 李华
网站建设 2026/4/30 23:36:09

语音转写技术在专业服务领域的应用实践

作为专业服务从业者&#xff0c;高效的信息记录与处理能力直接影响工作质量。以留学咨询行业为例&#xff0c;日常需要处理大量语音交流内容&#xff0c;传统手工记录方式不仅效率低下&#xff0c;还容易遗漏关键信息。本文将结合技术实现原理&#xff0c;探讨语音转写工具在专…

作者头像 李华
网站建设 2026/5/2 2:55:25

Cartographer SLAM系统实战指南:从零构建智能地图

Cartographer SLAM系统实战指南&#xff1a;从零构建智能地图 【免费下载链接】cartographer 项目地址: https://gitcode.com/gh_mirrors/car/cartographer 在机器人技术和自动驾驶领域&#xff0c;精准的环境感知与定位是核心技术挑战。Cartographer作为谷歌开源的SLA…

作者头像 李华