我的流程很简单:用户单击一个按钮,出现一个QDialog弹出窗口,我希望渲染一个MenuBar和一个在MenuBar下面的图像(渲染发生在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_())
这里是我到目前为止(可编辑图像路径)的可复制示例,但是图像的一部分显示在菜单栏的后面。如何解决此问题?我认为主要问题在于rect()调用,因为它似乎正在使用整个窗口,但我希望菜单栏位于“窗口外部”
答案 0 :(得分:2)
一种可能的解决方案是给出一个偏移量,如S.Nick所示,但是如您所见,第一个缺点是计算取决于样式和OS的偏移量。因此,另一种可能的解决方案是在显示图像的地方创建另一个小部件,并将其添加到布局中:
# ...
class Widget(QWidget):
def __init__(self):
super().__init__()
self.pixmap = QPixmap(
r"C:\Users\kjankosk\Desktop\Panasonic-OLED-TV-FZ950-Lifestyle.jpg"
)
def paintEvent(self, event):
super().paintEvent(event)
painter = QtGui.QPainter(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
painter.drawPixmap(self.rect(), self.pixmap)
def sizeHint(self):
return self.pixmap.size()
class SecondExample(QDialog):
def __init__(self):
super().__init__()
layout = QHBoxLayout(self)
toolbar = QMenuBar()
toolbar.addAction(QAction("Edit", toolbar))
toolbar.addAction(QAction("Polygon", toolbar))
toolbar.addAction(QAction("Rectangle", toolbar))
layout.setMenuBar(toolbar)
widget = Widget()
layout.addWidget(widget)
# ...
答案 1 :(得分:0)
尝试一下:
import sys
from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QDialog,
QMenuBar, QAction, QHBoxLayout, QWidget, QGridLayout)
class SecondExample(QDialog):
def __init__(self):
super().__init__()
self.installEventFilter(self)
toolbar = QMenuBar()
toolbar.addAction(QAction("Edit", toolbar))
toolbar.addAction(QAction("Polygon", toolbar))
toolbar.addAction(QAction("Rectangle", toolbar))
layout = QHBoxLayout()
layout.setMenuBar(toolbar)
self.setLayout(layout)
self.pixmap = QPixmap("D:/_Qt/__Qt/img/max1.jpg") # py-qt.png
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)
rect = self.rect().x(), self.rect().y()+20, self.rect().width(), self.rect().height()-20 # +++
painter.drawPixmap(*rect, self.pixmap) # +++
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.button = QPushButton(self)
self.button.setText("Click")
self.button.setMaximumHeight(100)
self.button.setMaximumWidth(100)
self.button.clicked.connect(self.clicked)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
layout = QGridLayout(centralWidget)
# self.layout().addWidget(self.button)
layout.addWidget(self.button)
def clicked(self):
self.something = SecondExample()
self.something.show() # exec()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())