• Qt第六十五章:自定义菜单栏的隐藏、弹出


    目录

    一、效果图

    二、qtDesigner

    三、ui文件如下:

    四、代码


     

    一、效果图

    二、qtDesigner

    原理是利用属性动画来控制QFrame的minimumWidth属性。

    ①先拖出相应的控件

    ②布局一下

     ③填上一些样式

     

    相关QSS

    1. background-color: rgb(238, 242, 255);
    2. border:2px solid rgb(255, 255, 255);
    3. border-radius:15px
    1. QFrame{
    2. background-color: qradialgradient(cx:0, cy:0, radius:1, fx:0.1, fy:0.1, stop:0 rgb(243, 175, 189), stop:1 rgb(155, 118, 218));
    3. border-top-left-radius:30px;
    4. border-top-right-radius:0px;
    5. border-bottom-right-radius:0px;
    6. border-bottom-left-radius:30px;
    7. }

    三、ui文件如下:

    1. "1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>Formclass>
    4. <widget class="QWidget" name="Form">
    5. <property name="geometry">
    6. <rect>
    7. <x>0x>
    8. <y>0y>
    9. <width>400width>
    10. <height>300height>
    11. rect>
    12. property>
    13. <property name="windowTitle">
    14. <string>Formstring>
    15. property>
    16. <layout class="QVBoxLayout" name="verticalLayout">
    17. <item>
    18. <widget class="QPushButton" name="pushButton">
    19. <property name="text">
    20. <string>PushButtonstring>
    21. property>
    22. widget>
    23. item>
    24. <item>
    25. <widget class="QFrame" name="frame">
    26. <property name="frameShape">
    27. <enum>QFrame::StyledPanelenum>
    28. property>
    29. <property name="frameShadow">
    30. <enum>QFrame::Raisedenum>
    31. property>
    32. <layout class="QHBoxLayout" name="horizontalLayout">
    33. <item>
    34. <widget class="QSplitter" name="splitter">
    35. <property name="orientation">
    36. <enum>Qt::Horizontalenum>
    37. property>
    38. <widget class="QLabel" name="label">
    39. <property name="styleSheet">
    40. <string notr="true">background-color: rgb(238, 242, 255);
    41. border:2px solid rgb(255, 255, 255);
    42. border-radius:15pxstring>
    43. property>
    44. <property name="text">
    45. <string>TextLabelstring>
    46. property>
    47. widget>
    48. <widget class="QLabel" name="label_2">
    49. <property name="styleSheet">
    50. <string notr="true">background-color: rgb(238, 242, 255);
    51. border:2px solid rgb(255, 255, 255);
    52. border-radius:15pxstring>
    53. property>
    54. <property name="text">
    55. <string>TextLabelstring>
    56. property>
    57. widget>
    58. widget>
    59. item>
    60. <item>
    61. <widget class="QFrame" name="frame_2">
    62. <property name="maximumSize">
    63. <size>
    64. <width>0width>
    65. <height>16777215height>
    66. size>
    67. property>
    68. <property name="styleSheet">
    69. <string notr="true">QFrame{
    70. background-color: qradialgradient(cx:0, cy:0, radius:1, fx:0.1, fy:0.1, stop:0 rgb(243, 175, 189), stop:1 rgb(155, 118, 218));
    71. border-top-left-radius:30px;
    72. border-top-right-radius:0px;
    73. border-bottom-right-radius:0px;
    74. border-bottom-left-radius:30px;
    75. }string>
    76. property>
    77. <property name="frameShape">
    78. <enum>QFrame::StyledPanelenum>
    79. property>
    80. <property name="frameShadow">
    81. <enum>QFrame::Raisedenum>
    82. property>
    83. widget>
    84. item>
    85. layout>
    86. widget>
    87. item>
    88. layout>
    89. widget>
    90. <resources/>
    91. <connections/>
    92. ui>

    四、代码

    使用uic工具将ui文件转成py文件

    1. import sys
    2. from PySide6.QtCore import QPropertyAnimation, QEasingCurve, QParallelAnimationGroup
    3. from PySide6.QtWidgets import *
    4. from zzz.ui_home_03 import Ui_Form
    5. # 继承UI类
    6. class MainWindow(QWidget, Ui_Form):
    7. def __init__(self, parent=None):
    8. super(MainWindow, self).__init__(parent)
    9. self.setupUi(self)
    10. self.pushButton.clicked.connect(self.settingBox)
    11. def settingBox(self):
    12. widthRightBox = self.frame_2.width()
    13. maxExtend = 100
    14. standard = 0
    15. if widthRightBox == 0:
    16. widthExtended = maxExtend
    17. else:
    18. widthExtended = standard
    19. # 创建属性动画
    20. self.right_box = QPropertyAnimation(self.frame_2, b"minimumWidth")
    21. self.right_box.setDuration(500)
    22. self.right_box.setStartValue(widthRightBox)
    23. self.right_box.setEndValue(widthExtended)
    24. self.right_box.setEasingCurve(QEasingCurve.InOutQuart)
    25. self.right_box.start()
    26. # 动画组 如果是多个动画同时执行,则创建动画组。
    27. # self.group = QParallelAnimationGroup()
    28. # self.group.addAnimation(self.right_box)
    29. # self.group.start()
    30. if __name__ == '__main__':
    31. app = QApplication()
    32. window = MainWindow()
    33. window.show()
    34. sys.exit(app.exec())

  • 相关阅读:
    MSQL系列(九) Mysql实战-Join算法底层原理
    三菱FX3U系列—小项目
    ubuntu Setforeground 前台应用切换
    mybatis-plus通过inSql实现子查询以及运算符
    今天写了一个可以测试并发数和运行次数的压力测试代码。(Java)
    2022年,软件测试已经不吃香了吗?
    NFV中:DPDK与SR-IOV应用场景及性能对比
    【大数据】9大实战项目解决你所有烦恼(写论文、找工作)
    在线教育项目【统一异常处理】
    vscode开发油猴插件环境配置指南
  • 原文地址:https://blog.csdn.net/wenxingchen/article/details/133959302