Pyside6 QMenuBar类提供了一个水平菜单栏,更多关于QMenuBar的使用可以参考下面的文档。

https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QMenuBar.html

QMenuBar使用

QMenuBar常用函数

QMenuBar常用信号

QMenuBar添加菜单项

使用addMenu可以为菜单栏添加子菜单

self.menu_bar = self.menuBar() # 获取主对话框的菜单栏

menu_setting = self.menu_bar.addMenu("设置") # 添加菜单
menu_appearance_setting = menu_setting.addMenu("外观设置") # 嵌套子菜单
menu_appearance_setting.addAction('软件主题设置')   # 菜单添加addAction

menu_color_setting= menu_setting.addMenu("颜色设置")
menu_color_setting.addAction('背景颜色设置')
menu_color_setting.addAction('字体颜色设置')


menu_edit = self.menu_bar.addMenu("编辑") # 添加菜单
menu_edit.addAction('复制')
menu_edit.addAction('粘贴')
menu_edit.addAction('剪切')
menu_edit.addAction('撤销')

Pyside6 QMenuBar-LMLPHP

QMenuBar添加图标

使用setIcon函数可以为菜单添加相应的图标,需要注意的是,当一级菜单设置了图标之后,其标题就不会显示。

self.menu_bar = self.menuBar() # 获取主对话框的菜单栏

menu_setting = self.menu_bar.addMenu("设置") # 添加菜单

menu_appearance_setting = menu_setting.addMenu("外观设置") # 嵌套子菜单
menu_appearance_setting_action = menu_appearance_setting.addAction('软件主题设置')   # 菜单添加addAction

menu_color_setting = menu_setting.addMenu("颜色设置")
menu_backgroundcolor_setting_action = menu_color_setting.addAction('背景颜色设置')
menu_fontcolor_setting_action = menu_color_setting.addAction('字体颜色设置')

menu_setting.setIcon(QIcon(':/resources/resource/icon/setting.ico'))
menu_appearance_setting.setIcon(QIcon(':/resources/resource/icon/setting.ico'))
menu_color_setting.setIcon(QIcon(':/resources/resource/icon/setting.ico'))
menu_appearance_setting_action.setIcon(QIcon(':/resources/resource/icon/setting.ico'))
menu_backgroundcolor_setting_action.setIcon(QIcon(':/resources/resource/icon/setting.ico'))
menu_fontcolor_setting_action.setIcon(QIcon(':/resources/resource/icon/setting.ico'))

Pyside6 QMenuBar-LMLPHP

QMenuBar添加菜单点击事件

创建菜单的最终目标是能够响应鼠标的点击事件,triggered信号可以在鼠标点击的时候触发响应。如果上级菜单也设置点击事件,那么下一级菜单点击事件响应完之后,再响应上一级菜单的点击事件。

menu_appearance_setting_action.triggered.connect(self.menu_appearance_setting_func)
menu_backgroundcolor_setting_action.triggered.connect(self.menu_backgroundcolor_setting_func)
menu_fontcolor_setting_action.triggered.connect(self.menu_fontcolor_setting_func)
menu_edit_copy_action.triggered.connect(self.menu_edit_copy_setting_func)
menu_edit_paste_action.triggered.connect(self.menu_edit_paste_setting_func)
menu_edit_cut_action.triggered.connect(self.menu_edit_cut_setting_func)
menu_edit_undo_action.triggered.connect(self.menu_edit_undo_setting_func)


    def menu_appearance_setting_func(self):
        print("软件主题设置")
        
    def menu_backgroundcolor_setting_func(self):
        print("背景颜色设置")

    def menu_fontcolor_setting_func(self):
        print("字体颜色设置")

    def menu_edit_copy_setting_func(self):
        print("复制")

    def menu_edit_paste_setting_func(self):
        print("粘贴")

    def menu_edit_cut_setting_func(self):
        print("剪切")

    def menu_edit_undo_setting_func(self):
        print("撤销")

Pyside6 QMenuBar-LMLPHP
完成程序可以通过百度网盘下载
链接:https://pan.baidu.com/s/1do1TFgjjPRkloa_BeoMQng
提取码:rk9s

10-24 03:36