sách gpt4 ăn đã đi

c++ - 固定 QGraphicsItem 位置,不改变场景中其他 QGraphicsItem 的行为

In lại 作者:太空狗 更新时间:2023-10-29 20:42:54 30 4
mua khóa gpt4 giày nike

这个问题是关于:Forcing QGraphicsItem To Stay Put

我想在场景中四处移动时在固定位置放置一个QGraphicsItem

建议的解决方案是覆盖子类 QGraphicsView của 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 的默认行为,还是有更简单的方法来做到这一点?

Cảm ơn.

câu trả lời hay nhất

我想这就是您要找的:

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

QGraphicsItem::ItemIgnoresTransformations

来自文档的描述:

The item ignores inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored). This flag is useful for keeping text label items horizontal and unscaled, so they will still be readable if the view is transformed. When set, the item's view geometry and scene geometry will be maintained separately. You must call deviceTransform() to map coordinates and detect collisions in the view. By default, this flag is disabled. This flag was introduced in Qt 4.3. Note: With this flag set you can still scale the item itself, and that scale transformation will influence the item's children.

您可能还想将平移到其他东西的所有东西作为父级。然后,您移动、缩放或旋转单个图形组以影响除“不可变形”对象之外的所有内容。

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 的好例子)

Hy vọng điều này sẽ giúp.

biên tập:

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

主要.cpp

#include 
#include "mygraphicsview.h"

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

return a.exec();
}

我的图形 View .h

#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#include
#include
#include

class MyGraphicsView : public QGraphicsView
{
Q_OBJECT

công cộng:
MyGraphicsView(QWidget *parent = 0);
~MyGraphicsView();

public slots:
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
riêng tư:
bool down;
QPointF m_last_pos;

QGraphicsItemGroup * m_group;
};

#endif // MYGRAPHICSVIEW_H

我的图形 View .cpp

#include "mygraphicsview.h"

#include
#include
#include

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());
}
}

关于c++ - 固定 QGraphicsItem 位置,不改变场景中其他 QGraphicsItem 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17373210/

30 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress