Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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中svg图像大小时出现的问题_Python_Svg_Pyqt_Qgraphicssvgitem - Fatal编程技术网

Python 使用矢量效果调整pyqt5中svg图像大小时出现的问题

Python 使用矢量效果调整pyqt5中svg图像大小时出现的问题,python,svg,pyqt,qgraphicssvgitem,Python,Svg,Pyqt,Qgraphicssvgitem,我在QGraphicsSvgItem上有QGraphicsSvgItem。我试图使用位于QGraphicsSvgItem边界矩形的夹点项(线)调整其大小。在test.svg文件中,我将向量效果属性设置为非缩放笔划。 当我通过拉伸夹点项来调整它的大小时,它会在边界矩形和svg图像的轮廓之间产生空白 下面是代码 import sys from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer from PyQt5.QtWidgets import Q

我在QGraphicsSvgItem上有QGraphicsSvgItem。我试图使用位于QGraphicsSvgItem边界矩形的夹点项(线)调整其大小。在
test.svg
文件中,我将向量效果属性设置为非缩放笔划。
当我通过拉伸夹点项来调整它的大小时,它会在边界矩形和svg图像的轮廓之间产生空白

下面是代码

import sys
from PyQt5.QtSvg import QGraphicsSvgItem, QSvgRenderer
from PyQt5.QtWidgets import QLineEdit, QGraphicsItem, QApplication, QMainWindow, QGraphicsScene, QGraphicsView, \
    QGraphicsPathItem
from PyQt5.QtGui import QPen, QColor, QCursor, QPainterPath
from PyQt5.QtCore import Qt, QRectF, QPointF



class SizeGripItem(QGraphicsPathItem):
    def __init__(self, annotation_item, index, direction=Qt.Horizontal, parent=None):
        super(QGraphicsPathItem, self).__init__(parent=parent)
        self.width = self.height = 0
        if direction is Qt.Horizontal:
            self.height = annotation_item.boundingRect().height()
        else:
            self.width = annotation_item.boundingRect().width()

        path = QPainterPath()
        path.addRect(QRectF(0, 0, self.width, self.height))
        self.m_annotation_item = annotation_item
        self._direction = direction
        self.m_index = index
        self.setPath(path)
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
        self.setAcceptHoverEvents(True)
        self.setPen(QPen(QColor("black"), 2))
        self.setBrush(QColor("red"))
        self.setZValue(2)


    @property
    def direction(self):
        return self._direction

    def update_path(self):
        if self._direction is Qt.Horizontal:
            self.height = self.m_annotation_item.boundingRect().height()
        else:
            self.width = self.m_annotation_item.boundingRect().width()
        path = QPainterPath()
        path.addRect(QRectF(0, 0, self.width, self.height))
        self.setPath(path)

    def update_position(self):
        """updates grip items
        """
        self.update_path()
        pos = self.m_annotation_item.mapToScene(self.point(self.m_index))
        x = self.m_annotation_item.boundingRect().x()
        y = self.m_annotation_item.boundingRect().y()
        pos.setX(pos.x() + x)
        pos.setY(pos.y() + y)
        self.setEnabled(False)
        self.setPos(pos)
        self.setEnabled(True)

    def point(self, index):
        """
        yields a list of positions of grip items in a node item
        """
        x = self.m_annotation_item.boundingRect().width()
        y = self.m_annotation_item.boundingRect().height()
        if 0 <= index < 4:
            return [
                QPointF(0, 0),
                QPointF(0, 0),
                QPointF(0, y),
                QPointF(x, 0)
            ][index]

    def hoverEnterEvent(self, event):
        if self._direction == Qt.Horizontal:
            self.setCursor(QCursor(Qt.SizeHorCursor))
        else:
            self.setCursor(QCursor(Qt.SizeVerCursor))
        super(SizeGripItem, self).hoverEnterEvent(event)

    def hoverLeaveEvent(self, event):
        self.setCursor(QCursor(Qt.ArrowCursor))
        super(SizeGripItem, self).hoverLeaveEvent(event)

    def itemChange(self, change, value):
        """
        Moves position of grip item on resize or reference circle's position change
        """
        if change == QGraphicsItem.ItemPositionChange and self.isEnabled():
            p = QPointF(self.pos())
            if self.direction == Qt.Horizontal:
                p.setX(value.x())
            elif self.direction == Qt.Vertical:
                p.setY(value.y())
            self.m_annotation_item.resize(self.m_index, p)
            return p
        return super(SizeGripItem, self).itemChange(change, value)

class NodeItem(QGraphicsSvgItem):

    def __init__(self, parent=None):
        QGraphicsSvgItem.__init__(self, parent)
        self.m_renderer = QSvgRenderer("test.svg")
        self.setSharedRenderer(self.m_renderer)
        self.rect = QRectF(0, 0, 150, 300)

        self.setFlags(QGraphicsSvgItem.ItemIsMovable |
                      QGraphicsSvgItem.ItemIsSelectable |
                      QGraphicsSvgItem.ItemSendsGeometryChanges)

        self.lineGripItems = []
        self.sizeGripItems = []

    def boundingRect(self):
        return self.rect

    def paint(self, painter, option, widget):
            self.m_renderer.render(painter, self.boundingRect())

    def resize(self, i, p):
        """Move grip item with changing rect of node item
        """
        x = self.boundingRect().x()
        y = self.boundingRect().y()
        width = self.boundingRect().width()
        height = self.boundingRect().height()
        p_new = self.sizeGripItems[i].pos()
        self.prepareGeometryChange()

        if i == 0 or i == 1:
            self.rect = QRectF(x + p.x() - p_new.x(), y + p.y() - p_new.y(), width - p.x() + p_new.x(),
                               height - p.y() + p_new.y())

        if i == 2 or i == 3:
            self.rect = QRectF(x, y, width + p.x() - p_new.x(), height + p.y() - p_new.y())

        self.updateSizeGripItem([i])

    def addGripItem(self):
        """adds grip items
        """
        if self.scene() and not self.lineGripItems:
            for i, (direction) in enumerate(
                    (
                            Qt.Vertical,
                            Qt.Horizontal,
                            Qt.Vertical,
                            Qt.Horizontal,
                    )
            ):
                item = SizeGripItem(self, i, direction)
                self.scene().addItem(item)
                self.sizeGripItems.append(item)

    def updateSizeGripItem(self, index_no_updates=None):
        """updates grip items
        """
        index_no_updates = index_no_updates or []
        for i, item in zip(range(len(self.sizeGripItems)), self.sizeGripItems):
            if i not in index_no_updates:
                item.update_position()

    def itemChange(self, change, value):
        """Overloads and extends QGraphicsSvgItem to also update gripitem
        """
        if change == QGraphicsItem.ItemPositionHasChanged:
            self.updateSizeGripItem()
            return
        if change == QGraphicsItem.ItemSceneHasChanged:
            self.addGripItem()
            self.updateSizeGripItem()
            return
        return super(NodeItem, self).itemChange(change, value)

if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = QMainWindow()
        window.show()
        scene = QGraphicsScene()
        scene.setSceneRect(0, 0, 200, 200)

        view = QGraphicsView()
        view.setScene(scene)
        window.setCentralWidget(view)
        scene.addItem(NodeItem())

        sys.exit(app.exec_())


请提供一个我用mre编辑了我的文章。@Eyllanesc没有矢量效果,效果很好。请提供一个我用mre编辑了我的文章。@Eyllanesc没有矢量效果,效果很好。