如何在 QMainWindow 中创建菜单栏

How to create a menu-bar in a QMainWindow

我正在使用 PyQT 编写一个小应用程序。我试图在应用程序顶部创建一个包含常用选项("File"、"Edit"、"Options" 等)的菜单栏,但是当我尝试添加时我的工具栏没有出现它到我的 QMainWindow class。我环顾四周,但我做错了什么对我来说并不明显。请注意,我曾尝试使用 QMainWindow.menuBar() 方法而不是创建 QToolbar,但如果我这样做,该栏仍然完全不可见。使用下面的代码,我至少得到一个空条,即使它是空的。下面的代码是这个问题的一个最小示例。我想知道我必须更改什么才能显示操作。

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush
from PyQt5.QtWidgets import QMainWindow, QGraphicsScene, QToolBar, QMenu, QAction, QGraphicsView, QApplication


class GraphWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.scene = QGraphicsScene()

        # a grid foreground
        self.scene.setBackgroundBrush(QBrush(Qt.lightGray, Qt.CrossPattern))
        self.grid = True

        # Create upper toolbar with menu options
        tb = QToolBar()
        menu = QMenu()
        db_action = QAction("Open file")
        db_action.setStatusTip("Select a file to use as a database")
        db_action.triggered.connect(self.open_new_db)
        menu.addAction(db_action)

        tb.addWidget(menu)
        tb.setAllowedAreas(Qt.TopToolBarArea)
        tb.setFloatable(False)
        tb.setMovable(False)
        self.addToolBar(tb)

        self.statusBar().showMessage("Ready")

        # Demonstrate the results from the input.

        graphics = QGraphicsView(self.scene)
        self.setCentralWidget(graphics)
        self.showFullScreen()

    def open_new_db(self):
        pass

    def keyPressEvent(self, e):
        # Currently, we respond to a press of the Escape key by closing the program.
        if e.key() == Qt.Key_Escape:
            self.close()

app = QApplication(sys.argv)
gr = GraphWindow()
sys.exit(app.exec_())

问题是由垃圾收集器引起的,一个变量只保留在创建的上下文中,并且由于您已经在构造函数中创建了它,所以该 python 方法的结尾将其消除。只有具有父元素的元素或属于 class 的元素才会保留,因此在此解决方案中使用第一个选项。

class GraphWindow(QMainWindow):
    def __init__(self):
        [...]
        # Create upper toolbar with menu options
        tb = QToolBar(self)
        menu = QMenu(self)
        db_action = QAction("Open file", self)
        db_action.setStatusTip("Select a file to use as a database")
        db_action.triggered.connect(self.open_new_db)
        menu.addAction(db_action)
        tb.addWidget(menu)
        tb.setAllowedAreas(Qt.TopToolBarArea)
        tb.setFloatable(False)
        tb.setMovable(False)
        self.addToolBar(tb)

        self.statusBar().showMessage("Ready")
        [...]

要在菜单栏中创建一个菜单,您必须给它一个标题。此外,Qt 不拥有菜单或操作的所有权,因此您必须给它们一个 parent 或以其他方式保留对它们的引用,这样它们就不会得到 garbage-collected。您的示例可以这样修复:

class GraphWindow(QMainWindow):
    def __init__(self):
        ...
        # Create menu options
        menubar = self.menuBar()
        menu = QMenu('File', self) # title and parent
        db_action = QAction("Open file", self) # title and parent
        db_action.setStatusTip("Select a file to use as a database")
        db_action.triggered.connect(self.open_new_db)
        menu.addAction(db_action)
        menubar.addMenu(menu)

        self.statusBar().showMessage("Ready")

请注意,工具栏根本不需要。实现相同目的的一种更短更简单的方法是更直接地添加菜单和操作,如下所示:

    menubar = self.menuBar()
    menu = menubar.addMenu('File')
    db_action = menu.addAction("Open file")
    db_action.setStatusTip("Select a file to use as a database")
    db_action.triggered.connect(self.open_new_db)

在这里,Qt 会在需要的地方自动设置 parents。