相关疑难解决方法(0)

在相对于QGraphicsView的静态位置绘制项目

我想画一个总是出现在我右上角的通知QGraphicsView.但是,QGraphicsItems位置在场景坐标中指定,因此如果用户平移/缩放以查看场景的不同部分,则此通知将移出屏幕.

我已经发现我可以通过在当前视图更改时移动和缩放通知来模拟此行为.但这似乎非常无效,而且根本不优雅.

似乎QGraphicsView应该支持这种行为.文档提到了一个ItemIsPanel听起来很有希望的旗帜,但在视图中没有提及静态放置.ItemIgnoresTransformations也有助于缩放/缩放,但不能平移.

是否有支持此行为的内置Qt功能?

c++ qt qgraphicsview qgraphicsitem qgraphicsscene

8
推荐指数
1
解决办法
3927
查看次数

修复了QGraphicsItem位置,而不更改场景中其他QGraphicsItems的行为

该问题与以下问题有关:强制QGraphicsItem保持不变

QGraphicsItem在场景中移动时,我想在一个固定的位置。

建议的解决方案是覆盖void paintEvent(QPaintEvent*)子类的QGraphicsView

void MyGraphicsView::paintEvent(QPaintEvent*) {
  QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
  myItem->setPos(scenePos);
}
Run Code Online (Sandbox Code Playgroud)

但是,问题是我希望场景中的其他所有内容保持不变,即,如果缩放或移动,我希望所有其他内容都QGraphicsItems保持默认状态。

解决此问题的一种较差的方法是void QGraphicsView::paintEvent(QPaintEvent*)从内部调用void MyGraphicsView::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);
}
Run Code Online (Sandbox Code Playgroud)

但是,这会增加闪烁的行为,my_item因为它首先使用QGraphicsView::paintEvent(event);然后使用添加的代码进行定位

QPointF scenePos = mapToScene(0,0); // map viewport's top-left corner to scene
myItem->setPos(scenePos);
Run Code Online (Sandbox Code Playgroud)

问题是,我是否必须void MyGraphicsView::paintEvent(QPaintEvent*)从头开始重新实现并myItem为所有其他代码的期望行为和默认行为编写代码QGraphicsItems,还是有更简单的方法来做到这一点?

谢谢。

c++ qt qgraphicsview qgraphicsitem qgraphicsscene

3
推荐指数
1
解决办法
4512
查看次数

标签 统计

c++ ×2

qgraphicsitem ×2

qgraphicsscene ×2

qgraphicsview ×2

qt ×2