简体   繁体   中英

How to use itemChange from QGraphicsItem in Qt

I have custom ellipse QGraphicsItem class and custom line class. On scene I have let's say two ellipses and connection made between them by a line. Ellipse has a pointer to this line and is movable. My problem is that I dont know how to use itemChange() from QGraphicsItem . I want to make connection which will be changing with ellipse movement. So I want to use itemChange() method to change line coordinates in a way that it always will be in center of ellipse. I read documentation from QGraphicsItem::itemChange() but I don't know how to use it in my case.

As others have already noted, you need to override (re-implement) the method in your class.

Below is a fully working example demonstrating this:

#include "Dialog.h"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsEllipseItem>
#include <QGraphicsLineItem>


class CustomElipse : public QGraphicsEllipseItem
{
public:
    CustomElipse (const QRectF& rect) : QGraphicsEllipseItem(rect) {
        setFlag(QGraphicsItem::ItemIsMovable);
        setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
    }

    void addLine(QGraphicsLineItem *line, bool isPoint1) {
        this->line = line;
        isP1 = isPoint1;
    }

    QVariant itemChange(GraphicsItemChange change, const QVariant &value)
    {
        if (change == ItemPositionChange && scene()) {
            // value is the new position.
            QPointF newPos = value.toPointF();

            moveLineToCenter(newPos);
        }
        return QGraphicsItem::itemChange(change, value);
    }

    void moveLineToCenter(QPointF newPos) {
        // Converts the elipse position (top-left)
        // to its center position
        int xOffset = rect().x() + rect().width()/2;
        int yOffset = rect().y() + rect().height()/2;

        QPointF newCenterPos = QPointF(newPos.x() + xOffset, newPos.y() + yOffset);

        // Move the required point of the line to the center of the elipse
        QPointF p1 = isP1 ? newCenterPos : line->line().p1();
        QPointF p2 = isP1 ? line->line().p2() : newCenterPos;

        line->setLine(QLineF(p1, p2));
    }

private:
    QGraphicsLineItem *line;
    bool isP1;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene scene;

    CustomElipse *elipse1 = new CustomElipse(QRectF(30, 30, 15, 25));
    scene.addItem(elipse1);

    CustomElipse *elipse2 = new CustomElipse(QRectF(70, 70, 25, 15));
    scene.addItem(elipse2);

    QGraphicsLineItem *line = scene.addLine(QLineF(40, 40, 80, 80));

    elipse1->addLine(line, true);
    elipse2->addLine(line, false);

    QGraphicsView view(&scene);
    view.show();

    return a.exec();
}

The code above draws two movable elipses with a line that's drawn between them. The line position adjusts to follow the elipses.

When running, you get something like this: 省略号和线的预览

For anyone looking for python code here's the translation of @AntonyG 's code:

import sys
from PyQt5 import QtWidgets, QtCore


class CustomItem(QtWidgets.QGraphicsEllipseItem):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setFlag(self.ItemIsMovable)
        self.setFlag(self.ItemSendsGeometryChanges)
        self.line = None
        self.isPoint = None

    def addLine(self, line, ispoint):
        self.line = line
        self.isPoint = ispoint

    def itemChange(self, change , value):

        if change == self.ItemPositionChange and self.scene():
            newPos = value

            self.moveLineToCenter(newPos)

        return super(CustomItem, self).itemChange(change, value)

    def moveLineToCenter(self, newPos):
        xOffset = self.rect().x() + self.rect().width()/2
        yOffset = self.rect().y() + self.rect().height()/2

        newCenterPos = QtCore.QPointF(newPos.x()+xOffset, newPos.y()+yOffset)

        p1 = newCenterPos if self.isPoint else self.line.line().p1()
        p2 = self.line.line().p2() if self.isPoint else newCenterPos

        self.line.setLine(QtCore.QLineF(p1, p2))


def main():
    app =QtWidgets.QApplication(sys.argv)
    scene = QtWidgets.QGraphicsScene()

    ellipse = CustomItem(QtCore.QRectF(30, 30, 15, 25))
    scene.addItem(ellipse)

    ellipse2 = CustomItem(QtCore.QRectF(70, 70, 25, 15))
    scene.addItem(ellipse2)

    line = scene.addLine(QtCore.QLineF(40, 40, 80, 80))

    ellipse.addLine(line, True)
    ellipse2.addLine(line, False)

    view = QtWidgets.QGraphicsView(scene)
    view.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM