I have a simple flow: user clicks a button, a QDialog popup appears and I wish to render a MenuBar and an image below the MenuBar (the rendering happens during the paintEvent).
import sys
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QDialog, QMenuBar, QAction, QHBoxLayout
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.button = QPushButton()
self.button.setText("Click")
self.button.setMaximumHeight(100)
self.button.setMaximumWidth(100)
self.button.clicked.connect(self.clicked)
self.layout().addWidget(self.button)
self.show()
def clicked(self):
self.something = SecondExample()
self.something.exec()
class SecondExample(QDialog):
def __init__(self):
super().__init__()
self.installEventFilter(self)
layout = QHBoxLayout()
toolbar = QMenuBar()
toolbar.addAction(QAction("Edit", toolbar))
toolbar.addAction(QAction("Polygon", toolbar))
toolbar.addAction(QAction("Rectangle", toolbar))
layout.setMenuBar(toolbar)
self.setLayout(layout)
self.pixmap = QPixmap(r"C:\Users\kjankosk\Desktop\Panasonic-OLED-TV-FZ950-Lifestyle.jpg")
self.resize(self.pixmap.width(), self.pixmap.height())
def paintEvent(self, event):
super().paintEvent(event)
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawPixmap(self.rect(), self.pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Here is a reproducible example of what I have so far (edit the image path), however part of the image appears behind the menu bar. How can I fix this issue? I think the main problem is with the rect() call as it seems to be using the whole window, but I was hoping that the menu bar would be "outside the window".