python - 在 PyQt 中的菜单栏下方显示图像

标签 python python-3.x pyqt pyqt5 qmenubar

我有一个简单的流程:用户单击一个按钮,出现一个 QDialog 弹出窗口,我希望渲染一个菜单栏和菜单栏下方的图像(渲染发生在 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() 调用,因为它似乎使用整个窗口,但我希望菜单栏位于“窗口之外”。

最佳答案

一种可能的解决方案是提供一个像 S.Nick 所示的偏移量。但正如您首先看到的缺点是计算取决于样式和操作系统的偏移量。因此,另一种可能的解决方案是创建另一个显示图像的小部件并将其添加到布局中:

# ...
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)
# ...

关于python - 在 PyQt 中的菜单栏下方显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668095/

相关文章:

python - 使用 PyQt5 的 Discord 机器人控制面板

c++ - QWidgetAction 在 trigger() 之后保持可见

python - Pandas:旋转和绘图工作流程

javascript - 如何在odoo中实现recaptcha

python - sys.getsizeof 的深层版本

python - 计算一个元素在 python 列表中连续出现 n 次的次数

python - 在 QStyledItemDelegate 中使用信号 closeEditor 的正确方法?

python - 使用 getattr 获取包含在描述符中的方法

python - 分组并重命名 pandas 数据框

python - 空列表的正确类型提示是什么?