如何在QMainWindow

时间:2017-11-20 13:49:21

标签: python pyqt pyqt5 menubar qmainwindow

我正在使用PyQT编写一个小应用程序。我正在尝试在应用程序顶部创建一个菜单栏,其中包含常用选项("文件","编辑","选项"等等)但我的当我尝试将它添加到我的QMainWindow类时,工具栏没有出现。我环顾四周,但我不明白我做错了什么。请注意,我尝试使用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_())

2 个答案:

答案 0 :(得分:2)

要在菜单栏中创建菜单,您必须为其指定标题。此外,Qt不会获取菜单或操作的所有权,因此您必须为其提供父级或以其他方式保留对它们的引用,以便它们不会被垃圾收集。您的示例可以像这样修复:

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会自动在任何需要的地方设置父母。

答案 1 :(得分:1)

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

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")
        [...]
相关问题