将图像插入 QGridLayout 并在 PyQt5 中的图像顶部绘制

Posted

技术标签:

【中文标题】将图像插入 QGridLayout 并在 PyQt5 中的图像顶部绘制【英文标题】:Insert Image into QGridLayout and Draw on top of image in PyQt5 【发布时间】:2019-05-17 22:16:52 【问题描述】:

我是 PyQt 的新手,我正在尝试制作一个应用程序,左侧有一个 QPixmap,可以在上面绘制,右侧有一个 QTextEdit(用于简单的 OCR GUI)。 我在看: PyQt5 Image and QGridlayout 但我无法将它与下面的代码联系起来(我的头发都在抓挠!!) 当我尝试调整以下代码时,我得到的是一个 QMainWindow,其背景为 QPixmap,可以用鼠标绘制,并且 QPixmap 第二次出现在正确的位置,无法绘制。有人可以告诉我我做错了什么吗? 非常感谢!

# https://***.com/questions/51475306/
import sys
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication,QGridLayout, QLabel, QWidget, QTextEdit
from PyQt5.QtGui import QPixmap, QPainter, QPen

class Menu(QMainWindow):

    def __init__(self):
        super().__init__()
        self.drawing = False
        self.lastPoint = QPoint()
        self.image = QPixmap("S3.png")
        self.setGeometry(100, 100, 500, 300)
        self.resize(self.image.width(), self.image.height()) 
        layout = QGridLayout()
# Add a QTextEdit box 
        self.edit = QTextEdit()
        layout.addWidget(self.edit, 0, 0, 10, 20)
# This:
# https://***.com/questions/52616553
# indicates that a QPixmap must be put into a label to insert into a QGridLayout
        self.label = QLabel()
        self.label.setPixmap(self.image)
        layout.addWidget(self.label, 10, 20, 10, 20)

# https://***.com/questions/37304684/
        self.widget = QWidget()
        self.widget.setLayout(layout)
        self.setCentralWidget(self.widget)

        self.show()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(self.rect(), self.image)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drawing = True
            self.lastPoint = event.pos()
            print(self.lastPoint)

    def mouseMoveEvent(self, event):
        if event.buttons() and Qt.LeftButton and self.drawing:
            painter = QPainter(self.image)
            painter.setPen(QPen(Qt.red, 3, Qt.SolidLine))
            painter.drawLine(self.lastPoint, event.pos())
            print(self.lastPoint,event.pos())
            self.lastPoint = event.pos()
            self.update()

     def mouseReleaseEvent(self, event):
         if event.button == Qt.LeftButton:
             self.drawing = False

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainMenu = Menu()
    sys.exit(app.exec_())

【问题讨论】:

【参考方案1】:

每个小部件都必须完成特定的任务,所以我创建了一个只有绘制功能的小部件,主小部件作为绘画小部件和 QTextEdit 的容器。

from PyQt5 import QtCore, QtGui, QtWidgets


class Label(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Label, self).__init__(parent)
        self.image = QtGui.QPixmap("S3.png")
        self.drawing = False
        self.lastPoint = QtCore.QPoint()

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(QtCore.QPoint(), self.image)

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self.drawing = True
            self.lastPoint = event.pos()

    def mouseMoveEvent(self, event):
        if event.buttons() and QtCore.Qt.LeftButton and self.drawing:
            painter = QtGui.QPainter(self.image)
            painter.setPen(QtGui.QPen(QtCore.Qt.red, 3, QtCore.Qt.SolidLine))
            painter.drawLine(self.lastPoint, event.pos())
            self.lastPoint = event.pos()
            self.update()

    def mouseReleaseEvent(self, event):
        if event.button == QtCore.Qt.LeftButton:
            self.drawing = False

    def sizeHint(self):
        return self.image.size()


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.label = Label()
        self.textedit = QtWidgets.QTextEdit()

        widget = QtWidgets.QWidget()
        self.setCentralWidget(widget)
        lay = QtWidgets.QHBoxLayout(widget)
        lay.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
        lay.addWidget(self.textedit)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

【讨论】:

我感觉原作者可能会回复我的信息,所以非常非常感谢eyllanesc。我看了很多你的相关代码,无法连接点,但这个例子真的帮助了我。再次感谢

以上是关于将图像插入 QGridLayout 并在 PyQt5 中的图像顶部绘制的主要内容,如果未能解决你的问题,请参考以下文章

如何将pyqt5中的后退按钮和前进按钮添加到我的QGridLayout

PyQt - QGridLayout 设置第一行样式表

PyQt5 QGridLayout 大小不正确

PyQt:QGridLayout 中的 setSpacing

在pyqt中添加两个布局

PyQt4:根据 QMainWindow 的大小设置 QGridLayout 的大小