如何使用itemChange从QGraphicsItem在Qt [英] How to use itemChange from QGraphicsItem in Qt

查看:3191
本文介绍了如何使用itemChange从QGraphicsItem在Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自定义椭圆 QGraphicsItem 类和自定义线类。在现场我让我们说两个椭圆和它们之间的连接由一条线。椭圆有一个指向这条线的指针,是可移动的。我的问题是,我不知道如何使用 itemChange() QGraphicsItem 。我想做连接,将随着椭圆运动而改变。所以我想使用 itemChange()方法来改变线坐标,它总是在椭圆的中心。我阅读了来自


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 to 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:

这篇关于如何使用itemChange从QGraphicsItem在Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆