固定QGraphicsItem位置,同时不影响场景中其他QGraphicsItem的行为

5
这个问题与强制QGraphicsItem保持不动有关。
我希望在场景中移动时,QGraphicsItem能够固定在一个位置上。
建议的解决方案是重写子类化的QGraphicsViewvoid paintEvent(QPaintEvent*)
void MyGraphicsView::paintEvent(QPaintEvent*) {
  QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
  myItem->setPos(scenePos);
}

然而,问题在于我希望场景中的其他所有内容保持不变,即如果我缩放或移动,我希望所有其他QGraphicsItems表现为默认状态。
解决此问题的一种不好的方法是从void MyGraphicsView::paintEvent(QPaintEvent*)中调用void QGraphicsView::paintEvent(QPaintEvent*)
void MyGraphicsView::paintEvent(QPaintEvent* event) {
  QGraphicsView::paintEvent(event);

  QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
  myItem->setPos(scenePos);
}

然而,这会给my_item添加闪烁行为,因为它首先使用QGraphicsView::paintEvent(event);定位,然后再使用添加的代码。
QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
myItem->setPos(scenePos);

问题是,我是否需要从头重新实现void MyGraphicsView::paintEvent(QPaintEvent*),并编写myItem所需行为和所有其他QGraphicsItems的默认行为的代码,还是有更简单的方法可以做到这一点?
谢谢。

为什么你在调用 setPos() 后不调用 paintEvent() 呢? - Chris
@Chris 这会产生更糟糕的影响。在屏幕上显示物品的痕迹。 - Man of One Way
您可以使用[QGraphicsItem :: itemChange](http://qt-project.org/doc/qt-5.0/qtwidgets/qgraphicsitem.html#itemChange)函数并查看是否在那里获得了一些通知吗?也许`QGraphicsItem :: ItemScenePositionHasChanged就是您需要的那个。您必须启用QGraphicsItem :: ItemSendsScenePositionChanges`标志才能正常工作。 - thuga
请参见以下程序相关内容:https://dev59.com/LmMl5IYBdhLWcg3wbGh1 - Trilarion
1个回答

8

我认为这就是你要找的东西:

http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#setFlag

QGraphicsItem::ItemIgnoresTransformations

从文档中的描述:

该项忽略继承的变换(即其位置仍然锚定在其父项上,但父项或视图的旋转、缩放或剪切变换被忽略)。此标志对于保持文本标签项目水平和未缩放非常有用,因此如果视图被转换,它们仍将可读。设置后,项目的视图几何和场景几何将分别维护。您必须调用deviceTransform()以映射坐标并检测视图中的碰撞。默认情况下,此标志已禁用。该标志是在Qt 4.3中引入的。注意:设置此标志后,您仍然可以缩放项目本身,并且该缩放变换将影响项目的子项。

您可能还希望将所有可以移动的内容都与其他内容进行关联。然后,您只需移动、缩放或旋转单个图形组,就可以影响除了“不可变换”对象之外的所有内容。

https://qt-project.org/doc/qt-4.8/graphicsview.html#the-graphics-view-coordinate-system

https://qt-project.org/doc/qt-4.8/painting-transformations.html (一个很棒的例子,但它并没有真正展示这个特性)

http://qt-project.org/doc/qt-4.8/demos-chip.html (使用QGraphicsView的绝佳例子)

希望有所帮助。

编辑:

示例展示如何使用parenting实现静态图层:

main.cpp

#include <QApplication>
#include "mygraphicsview.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyGraphicsView w;
    w.show();

    return a.exec();
}

mygraphicsview.h

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QGraphicsItemGroup>
#include <QMouseEvent>

class MyGraphicsView : public QGraphicsView
{
    Q_OBJECT

public:
    MyGraphicsView(QWidget *parent = 0);
    ~MyGraphicsView();

public slots:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
private:
    bool down;
    QPointF m_last_pos;

    QGraphicsItemGroup * m_group;
};

#endif // MYGRAPHICSVIEW_H

mygraphicsview.cpp

#include "mygraphicsview.h"

#include <QGraphicsItem>
#include <QGraphicsEllipseItem>
#include <QGraphicsTextItem>

MyGraphicsView::MyGraphicsView(QWidget *parent)
    : QGraphicsView(parent)
{
    down = false;
    this->setScene(new QGraphicsScene);

    // Anything not added to the "group" will stay put
    this->scene()->addEllipse(20, 20, 50, 50);
    this->scene()->addEllipse(180, 180, 50, 50);
    this->scene()->addText("Click and drag with the mouse to move only the tiny dots.");

    // This group will receive all transformations
    m_group = new QGraphicsItemGroup;
    for(int r = 0; r < 20; r ++)
    {
        for(int c = 0; c < 20; c++)
        {
            if(c % 5 == 0 && r % 5 == 0)
            {
                QGraphicsTextItem * txt = new QGraphicsTextItem(QString::number(r) + "," + QString::number(c));
                m_group->addToGroup(txt);
                txt->setPos(r*100, c*100);
            }
            m_group->addToGroup(new QGraphicsEllipseItem(r *100, c*100, 5, 5));
        }
    }

    this->scene()->addItem(m_group);
}

MyGraphicsView::~MyGraphicsView()
{

}

void MyGraphicsView::mousePressEvent(QMouseEvent *event)
{
    m_last_pos = mapToScene(event->pos());
    down = true;
}

void MyGraphicsView::mouseReleaseEvent(QMouseEvent *)
{
    down = false;
}

void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
    if(down)
    {
        QPointF temp = mapToScene(event->pos());

        QPointF delta = temp - m_last_pos;
        m_last_pos = temp;

        // Apply transformation to the group, not the scene!
        m_group->translate(delta.x(), delta.y());
    }
}

抱歉,ItemIgnoresTransformations已经设置过了。这对在场景中移动没有帮助。 - Man of One Way
你尝试过将其他所有内容都作为另一个QGraphicsItemGroup的子项,然后对其应用平移而不是对场景进行平移吗? - phyatt
那还得让我从头开始实现 void MyGraphicsView::paintEvent(QPaintEvent*),对吧? - Man of One Way
我会在接下来的几分钟内快速编写一个示例。如果你正确地进行了父子关系的处理,你就不需要这样做。 - phyatt

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