pyqt5 在 QGraphicsScene 上用按钮画线

pyqt5 drawing lines with a button on a QGraphicsScene

我对 pyqt5 有疑问。我创建了一个 windows 场景,该场景具有背景图像,重新实现了 drawBackground。我还有一个按钮,允许我在场景中的某个位置添加一条线。问题是,如果我点击按钮画线,那么这条线是在一个单独的场景中绘制的,它有自己的背景,而不是我的场景。似乎它创建了一个新场景来划清界线。这是我的代码:

import sys

from PyQt5 import QtGui
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import (QMainWindow, QGraphicsView, QPushButton, 
    QHBoxLayout, QVBoxLayout, QWidget, QApplication, QGraphicsScene)

class GraphicsScene(QGraphicsScene):

  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self._image = QImage()

  @property
  def image(self):
    return self._image

  @image.setter
  def image(self, img):
    self._image = img
    self.update()

  def drawBackground(self, painter, rect):
    if self.image.isNull():
      super().drawBackground(painter, rect)
    else:
      painter.drawImage(rect, self._image)

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.title = "parcelDeliveryIta";
        self.top = 100
        self.left = 100
        self.width = 1500
        self.height = 900
        self.initUI()

    def initUI(self):

        self.scene = GraphicsScene(self)
        self.scene._image = QImage('Italy.png')
        view = QGraphicsView(self.scene, self)
        self.scene.setSceneRect(0, 0, view.width(), view.height())

        addLine = QPushButton('AddLine')
        addLine.clicked.connect(self.addLine)

        hbox = QHBoxLayout(self)
        hbox.addWidget(view)

        vbox = QVBoxLayout(self)
        vbox.addWidget(addLine)

        hbox.addLayout(vbox)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.setFixedSize(self.width, self.height)
        self.setLayout(hbox)
        self.show()

    def addLine(self):
        self.scene.addLine(0, 0, 100, 100)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

这是点击按钮时的结果:

可以看出,线条是用自己的背景绘制的,也就是我设置为场景背景的图像(上面的图像被裁剪以更好地显示线条) 感谢您的帮助。

解释:

好像 is not suitable for your case since as the docs指出:

void QGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)

Draws the background of the scene using painter, before any items and the foreground are drawn. Reimplement this function to provide a custom background for the scene.

All painting is done in scene coordinates. The rect parameter is the exposed rectangle.

If all you want is to define a color, texture, or gradient for the background, you can call setBackgroundBrush() instead.

See also drawForeground() and drawItems().

(强调我的)

该油漆也将用于绘制项目的底部,因此导致该行为。


解决方案:

因此您将不得不求助于另一种解决方案,例如使用 QGraphicsPixmapItem 作为基础并使用该信息重新调整 window 的大小:

import sys

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import (
    QGraphicsView,
    QPushButton,
    QHBoxLayout,
    QVBoxLayout,
    QWidget,
    QApplication,
    QGraphicsScene,
)


class GraphicsView(QGraphicsView):
    def __init__(self, parent=None):
        super().__init__(parent)
        scene = QGraphicsScene(self)
        self.setScene(scene)
        self._pixmap_item = self.scene().addPixmap(QPixmap())
        self._pixmap_item.setZValue(-1)

    @property
    def pixmap(self):
        return self._pixmap_item.pixmap()

    @pixmap.setter
    def pixmap(self, pixmap):
        self._pixmap_item.setPixmap(pixmap)
        self.scene().setSceneRect(self._pixmap_item.boundingRect())

    def resizeEvent(self, event):
        if not self._pixmap_item.pixmap().isNull():
            self.fitInView(self._pixmap_item)
        super().resizeEvent(event)


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.title = "parcelDeliveryIta"
        self.top = 100
        self.left = 100
        self.width = 1500
        self.height = 900
        self.initUI()

    def initUI(self):

        self.view = GraphicsView(self)
        self.view.pixmap = QPixmap("Italy.png")

        addLine = QPushButton("AddLine")
        addLine.clicked.connect(self.addLine)

        hbox = QHBoxLayout(self)
        hbox.addWidget(self.view)

        vbox = QVBoxLayout()
        vbox.addWidget(addLine)

        hbox.addLayout(vbox)

        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.setFixedSize(self.width, self.height)
        self.show()

    def addLine(self):
        self.view.scene().addLine(0, 0, 100, 100)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())