【问题标题】:QGraphicsView->centerOn() doesn't workQGraphicsView->centerOn() 不起作用
【发布时间】:2014-04-18 04:38:09
【问题描述】:

我不知道如何让 centerOn() 移动项目周围的视口(或端口周围的项目,不确定它是哪种方式)。

以下代码在 main 中确实有效:

view.setDragMode(QGraphicsView::ScrollHandDrag);
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setFrameStyle(QFrame::NoFrame);
view.showFullScreen();

QGraphicsPixmapItem *pmItem = new QGraphicsPixmapItem(pixMap);
scene.addItem(pmItem);

view.centerOn(QPointF(50,30));
view.show();
rc = qA.exec(); 

但是,当我尝试从重载的 QGraphicsView 的paintEvent() 中执行相同操作时,centerOn() 什么也不做:

class MyView :: public QGraphicsView { ... }

void MyView::init(){
    ...
    setDragMode(QGraphicsView::ScrollHandDrag);
    setFrameStyle(QFrame::NoFrame);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    _scene.clear();
    setScene(&_scene);

    super::showNormal();

}


void MyView::paintEvent(QPaintEvent *aEvent){
    _scene.clear();
    QGraphicsPixmapItem *pmItem = new QGraphicsPixmapItem(pixMap);
    _scene.addItem(pmItem);
    centerOn(QPointF(50.0, 30.0));

    super::paintEvent(aEvent);
}

我错过了什么? 谢谢

亚历克斯

【问题讨论】:

  • 尝试重新实现函数drawForeground而不是paintEvent。这是您在视图中弄乱事物的通常方式。我猜想在paintEvent 运行时没有绘制视图,因此坐标 30.0 和 50.0 不是有效的
  • 抱歉没有早点回复。今晚我会检查一下,然后告诉你。谢谢。

标签: qt centering qgraphicsview qgraphicsitem qgraphicsscene


【解决方案1】:

你基本上是在paintEvent调用下面拉地毯。

QGraphicsView 通过信号和槽的方式与 QGraphicsScene 通信。场景没有机会通知视图刚刚添加了一个新项目,因为实际的paintEvent 在创建后立即被调用。两个步骤之间没有事件循环过程。因此,paintEvent 调用没有为新项目正确设置,更不用说中心更改了。

一旦绘画完成,视图就会被验证并认为它已经全部完成了。所以它永远不会更新新项目的区域。

在任何绘画或更新事件之外呼叫 centerOn。

【讨论】:

  • 抱歉没有早点回复。听起来很合理,尽管该项目确实在paintEvent 中更新和绘制。今晚我会检查一下,然后告诉你。谢谢。
  • 在 MouseMoveEvent 中运行良好。谢谢
【解决方案2】:

我通过对setSceneRect 设置高值解决了这个问题。 然后我将场景集中在一个对象或位置上。

示例:

this->ui->graphicsView->setSceneRect (0,0,100000000, 100000000);
this->ui->graphicsView->centerOn(500,1030);

setSceneRect 大小 GraphicsView 无法正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2015-10-18
    • 2015-05-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多