Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyqt5在QGRAPHICSCENE上使用按钮绘制线条_Python_Qt_Pyqt5_Qgraphicsview_Qgraphicsscene - Fatal编程技术网

Python pyqt5在QGRAPHICSCENE上使用按钮绘制线条

Python pyqt5在QGRAPHICSCENE上使用按钮绘制线条,python,qt,pyqt5,qgraphicsview,qgraphicsscene,Python,Qt,Pyqt5,Qgraphicsview,Qgraphicsscene,我对pyqt5有问题。我创建了一个带有背景图像的场景的窗口,重新实现了缺点。我还有一个按钮,可以在场景中的某个位置添加一行。问题是,如果我单击按钮绘制线,那么这条线将绘制在一个单独的场景中,该场景具有自己的背景,而不是绘制到我的场景中。似乎它创造了一个新的场景来画线。这是我的密码: import sys from PyQt5 import QtGui from PyQt5.QtGui import QImage from PyQt5.QtWidgets import (QMainWindow,

我对pyqt5有问题。我创建了一个带有背景图像的场景的窗口,重新实现了缺点。我还有一个按钮,可以在场景中的某个位置添加一行。问题是,如果我单击按钮绘制线,那么这条线将绘制在一个单独的场景中,该场景具有自己的背景,而不是绘制到我的场景中。似乎它创造了一个新的场景来画线。这是我的密码:

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_())
这是单击按钮时的结果:

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

说明: 这似乎不适合您的情况,因为正如所指出的:

使用画师绘制场景的背景, 在绘制任何项目和前景之前。重新实施这个 函数为场景提供自定义背景

所有绘制都在场景坐标中完成。rect参数是 暴露的矩形

如果您只想为对象定义颜色、纹理或渐变 在后台,您可以调用setBackgroundBrush()

另请参见drawForeground()和drawItems()

(强调矿山)

该绘制也将用于绘制项目的基础,因此导致该行为


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

导入系统 从PyQt5.QtGui导入QPixmap 从PyQt5.QtWidgets导入( QZ视图, QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QApplication, Qsscene, ) 类GraphicsView(QGraphicsView): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) 场景=Qgraphicscene(自) self.setScene(场景) self.\u pixmap\u item=self.scene().addPixmap(QPixmap()) self.\u pixmap\u item.setZValue(-1) @财产 def pixmap(自): 返回self.\u pixmap\u item.pixmap() @pixmap.setter def pixmap(自身,pixmap): self.\u pixmap\u item.setPixmap(pixmap) self.scene() def resizeEvent(自我,事件): 如果不是self.\u pixmap\u item.pixmap().isNull(): self.fitInView(self.\u pixmap\u项目) super().resizeEvent(事件) 类主窗口(QWidget): 定义初始化(自): super()。\uuuu init\uuuuu() self.title=“parcelDeliveryIta” self.top=100 self.left=100 自宽=1500 自身高度=900 self.initUI() def initUI(self): self.view=图形视图(self) self.view.pixmap=QPixmap(“Italy.png”) addLine=QPushButton(“addLine”) addLine.clicked.connect(self.addLine) hbox=QHBoxLayout(自身) 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.view.scene().addLine(0,0,100,100) 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) 窗口=主窗口() sys.exit(app.exec_()) 说明: 这似乎不适合您的情况,因为正如所指出的:

使用画师绘制场景的背景, 在绘制任何项目和前景之前。重新实施这个 函数为场景提供自定义背景

所有绘制都在场景坐标中完成。rect参数是 暴露的矩形

如果您只想为对象定义颜色、纹理或渐变 在后台,您可以调用setBackgroundBrush()

另请参见drawForeground()和drawItems()

(强调矿山)

该绘制也将用于绘制项目的基础,因此导致该行为


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

导入系统 从PyQt5.QtGui导入QPixmap 从PyQt5.QtWidgets导入( QZ视图, QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QApplication, Qsscene, ) 类GraphicsView(QGraphicsView): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) 场景=Qgraphicscene(自) self.setScene(场景) self.\u pixmap\u item=self.scene().addPixmap(QPixmap()) self.\u pixmap\u item.setZValue(-1) @财产 def pixmap(自): 返回self.\u pixmap\u item.pixmap() @pixmap.setter def pixmap(自身,pixmap): self.\u pixmap\u item.setPixmap(pixmap) self.scene() def resizeEvent(自我,事件): 如果不是self.\u pixmap\u item.pixmap().isNull(): self.fitInView(self.\u pixmap\u项目) super().resizeEvent(事件) 类主窗口(QWidget): 定义初始化(自): super()。\uuuu init\uuuuu() self.title=“parcelDeliveryIta” self.top=100 self.left=100 自宽=1500 自身高度=900 self.initUI() def initUI(self): self.view=图形视图(self) self.view.pixmap=QPixmap(“Italy.png”) addLine=QPushButton(“addLine”) addLine.clicked.connect(self.addLine) hbox=QHBoxLayout(自身) 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()