【问题标题】:Add more than one Qmenu from other classes in MainWindow在 MainWindow 中添加多个来自其他类的 Qmenu
【发布时间】:2019-08-27 10:57:01
【问题描述】:

我想在我的主窗口中有一个菜单栏,并且能够从其他类中设置菜单栏中的菜单。使用 setMenuWidget 命令将覆盖第一个菜单选项,如代码所示。在我设置菜单的类中,我想我可能只需要设置一个菜单而不是菜单栏,然后​​在主窗口中设置菜单栏。

这是我想要的,可以通过在类中填充单个菜单栏来实现,尽管我试图避免这种方法。

而是只显示第二个菜单

import sys
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets

class ToolBar0(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self)

        bar = self.menuBar() # don't think I need a menubar here
        file_menu = bar.addMenu('menu1')
        one = QAction('one', self)
        two = QAction('two', self)
        file_menu.addAction(one)
        file_menu.addAction(two)


class ToolBar1(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self)

        bar = self.menuBar() # don't think I need a menubar here
        file_menu = bar.addMenu('menu2')
        one = QAction('one', self)
        two = QAction('two', self)
        file_menu.addAction(one)
        file_menu.addAction(two)


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, parent=None)

        #should a menubar be set up here?

        #For seting widgets in main window
        self.Tool_Bar0 = ToolBar0(self)
        self.setMenuWidget(self.Tool_Bar0)

        ###menu_bar0 is over written
        self.Tool_Bar1 = ToolBar1(self)
        #self.setMenuWidget(self.Tool_Bar1)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # creating main window
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

【问题讨论】:

  • 好吧,据我了解——在 pyqt 中,您只有 1 个菜单栏,但其中可以有多个菜单。所以你想要分类的不是菜单栏,而是你添加到菜单栏self.FileMenu = self.MainMenu.addMenu('File') 的东西,就像在这个例子中你想要分类的是 FileMenu 而不是应该可行的 MainMenu。请注意,您可能必须创建一个通用例程,用这些对象填充您的 MainMenu,因为它可能是多步骤过程
  • 谢谢丹尼斯,我可以看到我需要在类中设置菜单,然后添加到主窗口中的菜单栏,但我不知道该怎么做
  • 实际上,您可以将 MenuBar 创建为一个自包含的类并简单地对其进行实例化,这需要将 MainWindow 函数中的 MenuBar 设置为单行,并允许您模块化它以防万一您希望在某个时间点交换它或需要专门处理它。我总是以这种方式对我的菜单/工具栏进行子类化,因为它将我的 MainWindow 简化为它的基本要素。所以基本上有 2 个类,一个用于处理菜单/工具栏,一个用于处理您的菜单/操作项,如 Isma 的回答中所述

标签: python class pyqt5 qmenu qmenubar


【解决方案1】:

您可以使用带有方法的基类来返回包含QAction 项目的QMenu 项目列表或QAction 项目列表,然后以您想要的任何方式将它们呈现在您的QMainWindow 工具栏中,这里是一个例子:

import sys
from PyQt5.QtWidgets import QAction, QApplication, QMainWindow, QMenu


class WindowWithToolbar:
    def __init__(self):
        super().__init__()

    def menu_items(self)->list:
        pass


class Window1(WindowWithToolbar, QMainWindow):
    def __init__(self):
        WindowWithToolbar.__init__(self)
        QMainWindow.__init__(self)

        # New menu with actions
        self.menu = QMenu('one')
        self.menu.addActions([QAction('two', self), QAction('three', self)])

    def menu_items(self):
        return [self.menu]


class Window2(WindowWithToolbar, QMainWindow):
    def __init__(self):
        WindowWithToolbar.__init__(self)
        QMainWindow.__init__(self)

    def menu_items(self):
        # Only actions
        return [QAction('three', self), QAction('four', self)]


class MainWindow(WindowWithToolbar, QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, parent=None)

        self.window1 = Window1()
        self.window2 = Window2()

        self.menu = QMenu('File')
        self.helloAction = QAction('Hello')
        self.menu.addAction(self.helloAction)

        self._build_menu()

    def menu_items(self)->list:
        return [self.menu]

    def _build_menu(self):
        self._add_menu_items(self)
        self._add_menu_items(self.window1)
        self._add_menu_items(self.window2)

    def _add_menu_items(self, windowWithToolbar: WindowWithToolbar):
        for menu_item in windowWithToolbar.menu_items():
            if isinstance(menu_item, QMenu):
                self.menuBar().addMenu(menu_item)
            elif isinstance(menu_item, QAction):
                self.menuBar().addAction(menu_item)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多