根据子'缩放器'项

时间:2016-06-15 21:35:56

标签: python c++ qt pyqt qgraphicsitem

我正在尝试创建一个最小的QGraphicsItem,它充当其父级的缩放器。我想我差不多了,但我正在绘制一个空白,以及如何在移动时将其位置传达给父项。我想要的是这样的(没有文字):

enter image description here

这是我迄今为止所拥有的一个独立的例子:

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *


class Resizer(QGraphicsEllipseItem):

    def __init__(self, rect=QRectF(0, 0, 10, 10), parent=None, scene=None):
        super().__init__(rect, parent, scene)

        self.setFlag(QGraphicsItem.ItemIsMovable, True)

    def mousePressEvent(self, mouseEvent):
        self.resize = True
        self.initialPos = self.scenePos()
        self.setSelected(True)

    def mouseMoveEvent(self, mouseEvent):
        if self.resize:
            self.currentPos = self.scenePos()
            self.change = self.initialPos - self.currentPos
        super().mouseMoveEvent(mouseEvent)

    def mouseReleaseEvent(self, mouseEvent):
        self.resize = False
        self.setSelected(False)


if __name__ == "__main__":

    app = QApplication(sys.argv)

    view = QGraphicsView()
    scene = QGraphicsScene()
    scene.setSceneRect(0, 0, 500, 500)
    view.setScene(scene)

    rect = QRectF(100, 100, 150, 50)
    box = QGraphicsRectItem(rect)

    scene.addItem(box)

    resizer = Resizer(parent=box)
    resizerWidth = resizer.rect().width() / 2
    resizerOffset = QPointF(resizerWidth, resizerWidth)
    resizer.setPos(box.rect().bottomRight() - resizerOffset)

    view.show()

    sys.exit(app.exec_())

那么我怎样才能将self.change中的mouseMoveEvent传达给父级,以便在移动Resizer时调整父级的大小?欢迎任何其他建议。

我尝试将Resizer转换为QGraphicsObject的子类,以便它可以在mouseMoveEvent上发出信号,但QGraphicsObject没有绘制方法,我不确定如何在场景中显示Resizer

欢迎提出任何建议。

1 个答案:

答案 0 :(得分:1)

我结束了Resizer类转换为QGraphicsObject的子类。事实证明有可能重新实现paint方法(即使我的自动完成之前没有找到它)。

Resizer.itemChange现在发出一个信号,该信号被分配给父resize类中的Box方法,该方法使用来自大小调整器的位置更改信息来调整方框的矩形。 / p>

这样,调整器也可以重复用于其他项目,而不必像提供的答案here那样重新实现鼠标按下/移动/释放事件。

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Box(QGraphicsRectItem):

    def __init__(self, position, rect=QRectF(0, 0, 100, 50), parent=None, scene=None):
        super().__init__(rect, parent, scene)

        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.ItemIsFocusable, True)
        self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)

        self.setPos(position)

        self.resizer = Resizer(parent=self)
        resizerWidth = self.resizer.rect.width() / 2
        resizerOffset = QPointF(resizerWidth, resizerWidth)
        self.resizer.setPos(self.rect().bottomRight() - resizerOffset)
        self.resizer.resizeSignal.connect(self.resize)

    def paint(self, painter, option, widget=None):
        pen = QPen()
        pen.setColor(Qt.black)
        painter.setPen(pen)
        painter.setBrush(Qt.transparent)
        painter.drawRect(self.rect())

    @pyqtSlot()
    def resize(self, change):
        self.setRect(self.rect().adjusted(0, 0, change.x(), change.y()))
        self.prepareGeometryChange()
        self.update()


class Resizer(QGraphicsObject):

    resizeSignal = pyqtSignal(QPointF)

    def __init__(self, rect=QRectF(0, 0, 10, 10), parent=None):
        super().__init__(parent)

        self.setFlag(QGraphicsItem.ItemIsMovable, True)
        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
        self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
        self.rect = rect

    def boundingRect(self):
        return self.rect

    def paint(self, painter, option, widget=None):
        if self.isSelected():
            pen = QPen()
            pen.setStyle(Qt.DotLine)
            painter.setPen(pen)
        painter.drawEllipse(self.rect)

    def itemChange(self, change, value):
        if change == QGraphicsItem.ItemPositionChange:
            if self.isSelected():
                self.resizeSignal.emit(value - self.pos())
        return value


if __name__ == "__main__":

    app = QApplication(sys.argv)

    view = QGraphicsView()
    scene = QGraphicsScene()
    scene.setSceneRect(0, 0, 500, 1000)
    view.setScene(scene)

    box = Box(QPointF(50, 50), scene=scene)

    view.show()

    sys.exit(app.exec_())