固定了QGraphicsItem的位置,而不改变场景中其他QGraphicsItems的行为

Fixed QGraphicsItem position, without changing behaviour of other QGraphicsItems in scene

本文关键字:其他 改变 QGraphicsItems QGraphicsItem 位置      更新时间:2023-10-16

此问题与:强制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的默认行为编写代码,或者有更简单的方法吗?

谢谢。

我想这就是您想要的:

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

QGraphicsItem::ItemIgnoresTransformations

文档描述:

该项忽略继承的变换(即,其位置仍固定到其父项,但忽略父项或视图的旋转、缩放或剪切变换)。此标志有助于保持文本标签项的水平和不缩放,因此在转换视图时它们仍然可读。设置后,项目的视图几何体和场景几何体将分别保留。必须调用deviceTransform()来映射坐标并检测视图中的碰撞。默认情况下,此标志处于禁用状态。此标志在问题4.3中引入。注意:设置此标志后,您仍然可以缩放项目本身,并且缩放转换将影响项目的子项。

你可能还想把所做的一切都归结为其他事情。然后,移动、缩放或旋转单个图形组,以影响除"不可变换"对象之外的所有对象。

https://qt-project.org/doc/qt-4.8/graphicsview.html#the-图形视图坐标系

https://qt-project.org/doc/qt-4.8/painting-transformations.html(一个很酷的例子,尽管它并没有真正显示这个功能)

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

希望能有所帮助。

编辑:

展示如何使用父子关系实现静态层的示例:

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());
    }
}
相关文章: