当前位置:   article > 正文

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

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. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Form</class>
  4. <widget class="QWidget" name="Form">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>400</width>
  10. <height>300</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>Form</string>
  15. </property>
  16. <layout class="QVBoxLayout" name="verticalLayout">
  17. <item>
  18. <widget class="QPushButton" name="pushButton">
  19. <property name="text">
  20. <string>PushButton</string>
  21. </property>
  22. </widget>
  23. </item>
  24. <item>
  25. <widget class="QFrame" name="frame">
  26. <property name="frameShape">
  27. <enum>QFrame::StyledPanel</enum>
  28. </property>
  29. <property name="frameShadow">
  30. <enum>QFrame::Raised</enum>
  31. </property>
  32. <layout class="QHBoxLayout" name="horizontalLayout">
  33. <item>
  34. <widget class="QSplitter" name="splitter">
  35. <property name="orientation">
  36. <enum>Qt::Horizontal</enum>
  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:15px</string>
  43. </property>
  44. <property name="text">
  45. <string>TextLabel</string>
  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:15px</string>
  53. </property>
  54. <property name="text">
  55. <string>TextLabel</string>
  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>0</width>
  65. <height>16777215</height>
  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::StyledPanel</enum>
  79. </property>
  80. <property name="frameShadow">
  81. <enum>QFrame::Raised</enum>
  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())

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/962328
推荐阅读
相关标签
  

闽ICP备14008679号