固定了QGraphicsItem的位置,而不改变场景中其他QGraphicsItems的行为
Fixed QGraphicsItem position, without changing behaviour of other QGraphicsItems in scene
此问题与:强制QGraphicsItem保持原位有关
当我在场景中移动时,我希望在固定的位置上有一个QGraphicsItem
。
建议的解决方案是覆盖子类别QGraphicsView
的void 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());
}
}
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 变量没有改变?通过向量的函数调用
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 在其他文件中创建类时在 c++ 项目中不起作用
- 类与私有变量的其他类之间的线程安全性
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- GlobalAlloc而不是其他分配方法
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 结构和双指针隐藏在其他结构中,多层混淆
- UE4在OnComponentBeginOverlap上铸造其他actor
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- MESI协议和std::atomic-它是否确保所有写入立即对其他线程可见?
- 固定了QGraphicsItem的位置,而不改变场景中其他QGraphicsItems的行为
- 静态方法中的对象创建是改变其他静态对象的私有成员变量
- 如何告诉c++编译器对象没有在其他地方被改变以达到更好的优化