I am writing a small application using PyQT. I am trying to create a menu bar at the top of the application containing the usual options ("File", "Edit", "Options" etc.) but my toolbar is not appearing when I try to add it to my QMainWindow class. I have looked around but it is not obvious to me what I am doing wrong. Note that I have tried using the QMainWindow.menuBar()
method instead of creating a QToolbar
, but if I do that the bar remains invisible altogether. Using the code below, I at least get an empty bar, even if it is empty. The code below is a minimal example of this problem. I would like to know what I have to change for the actions to show up.
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_())