如何在缩放视图时保持QGraphicsItem的大小和位置?

5

我在QGraphicsScene中有一些QGraphicsItems,需要在缩放时保持相同的大小和位置。我尝试过使用QGraphicsItem :: ItemIgnoresTransformations,但是物品的位置出现了错误。以下是一个示例代码:

我像这样子类化了QGraphicsView:

class Graphics : public QGraphicsView
{
public:
    Graphics();
    QGraphicsScene *scene;
    QGraphicsRectItem *rect;
    QGraphicsRectItem *rect2;

protected:
    void wheelEvent(QWheelEvent *event);
};

在它的构造函数中:

Graphics::Graphics()
{
    scene = new QGraphicsScene;
    rect = new QGraphicsRectItem(100,100,50,50);
    rect2 = new QGraphicsRectItem(-100,-100,50,50);
    scene->addLine(0,200,200,0);

    rect->setFlag(QGraphicsItem::ItemIgnoresTransformations, true);
    scene->addItem(rect);
    scene->addItem(rect2);

    setScene(scene);
    scene->addRect(scene->itemsBoundingRect());
}

wheelEvent虚函数:

void Graphics::wheelEvent(QWheelEvent *event)
{
    if(event->delta() < 0)
        scale(1.0/2.0, 1.0/2.0);
    else
        scale(2, 2);
    scene->addRect(scene->itemsBoundingRect());

    qDebug() << rect->transform();
    qDebug() << rect->boundingRect();
    qDebug() << rect2->transform();
    qDebug() << rect2->boundingRect();
}

原始视图如下所示: 1

将线路视为道路,将矩形视为符号。当缩小时,矩形保持其大小但跳出场景: 2

应该是矩形左上角到线的中心。我还困惑于调试信息显示boundingRect和transform保持不变,这似乎什么也没改变!是什么导致了问题?有没有办法解决它?可以有人帮忙吗?谢谢!

2个回答

2

抱歉耽误了时间,现在我自己解决了问题。

我发现QGraphicsItem::ItemIgnoresTransformations只有在你想要粘贴的点在物品坐标系中为(0,0)时才起作用。你还需要手动更新boundingRect。尽管如此,我找到的最好的解决方案是子类化QGraphicsItem,并根据世界矩阵在paint()中设置矩阵。下面是我的代码。

QMatrix stableMatrix(const QMatrix &matrix, const QPointF &p)
{
    QMatrix newMatrix = matrix;

    qreal scaleX, scaleY;
    scaleX = newMatrix.m11();
    scaleY = newMatrix.m22();
    newMatrix.scale(1.0/scaleX, 1.0/scaleY);

    qreal offsetX, offsetY;
    offsetX = p.x()*(scaleX-1.0);
    offsetY = p.y()*(scaleY-1.0);
    newMatrix.translate(offsetX, offsetY);

    return newMatrix;
}

还有绘制函数:

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                   QWidget *widget)
{
     QPointF p(left, top);
     painter->setMatrix(stableMatrix(painter->worldMatrix(), p));
     painter->drawRect(left, top, width, height);
}

stableMatrix的第二个参数是固定点,我的示例代码中它是项目左上角。您可以根据自己的喜好进行更改。这很有效!希望本帖对您有所帮助 :)

1
这个问题的解决方案更加简单。
QGraphicsItem :: ItemIgnoresTransformations 该项忽略继承的变换(即其位置仍然锚定在其父级上,但是父级或视图旋转、缩放或剪切变换被忽略)。[...]
这就是关键! 该项忽略所有变换,但仍绑定在其父级上。 因此,您需要两个项:一个父项将保持相对位置(未设置任何标志),而子项将在父项的(0,0)点进行绘制(设置了 QGraphicsItem :: ItemIgnoresTransformations 标志)。
下面是一些工作代码,用于实现具有恒定大小和旋转的十字线,同时保持相对于其父项的相对位置:
#include <QGraphicsItem>
#include <QPainter>

class CrossHair : public QGraphicsItem
{
private:
    class CrossHairImpl : public QGraphicsItem
    {
    public:
        CrossHairImpl (qreal len, QGraphicsItem *parent = nullptr)
            : QGraphicsItem(parent), m_len(len)
        {
            setFlag(QGraphicsItem::ItemIgnoresTransformations);
        }

        QRectF boundingRect (void) const override
        {
            return QRectF(-m_len, -m_len, m_len*2, m_len*2);
        }

        void paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
        {
            painter->setPen(QPen(Qt::red, 1));
            painter->drawLine(0, -m_len, 0, m_len);
            painter->drawLine(-m_len, 0, m_len, 0);
        }

    private:
        qreal m_len;
    };

public:
    CrossHair (qreal x, qreal y, qreal len, QGraphicsItem *parent = nullptr)
        : QGraphicsItem(parent), m_impl(len, this)  // <-- IMPORTANT!!!
    {
        setPos(x, y);
    }

    QRectF boundingRect (void) const override
    {
        return QRectF();
    }

    void paint (QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
    {
        // empty
    }

private:
    CrossHairImpl m_impl;
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接