【问题标题】:Glitchy grid in QGraphicsView::drawBackgroundQGraphicsView::drawBackground 中的故障网格
【发布时间】:2014-12-23 16:08:33
【问题描述】:

我正在尝试在QGraphicsView::drawBackground 上正确显示网格图案。在我尝试移动添加到场景中的项目之前,一切似乎都运行良好。

我在 MainWindow 中添加这样的行:

   QPen _Pen;
   _Pen.setColor(Qt::red);
   _Pen.setWidth(3);

   QGraphicsLineItem* _Line=new QGraphicsLineItem(0,0,100,100);
   _Line->setPen(_Pen);
   _Line->setVisible(true);
   _Line->setFlags(QGraphicsLineItem::ItemIsSelectable | QGraphicsLineItem::ItemIsMovable);

   m_scene->addItem(_Line);

GraphicsView的方法:

GraphicsView::GraphicsView() : cellSize(20)
{
   setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
}

void GraphicsView::drawBackground(QPainter *p, const QRectF &crect)
{
   p->save();
   p->setPen(QPen(Qt::black,1));

   for (int x = crect.topLeft().x(); x < crect.bottomRight().x(); x += cellSize)
      for (int y = crect.topLeft().y(); y < crect.bottomRight().y(); y += cellSize)
         p->drawPoint(x, y);

   p->restore();
}

问题可以看这里:

当我移动项目时,它会在其后面留下一串网格点,这些点与原始网格不对齐。

我不明白这个错误来自哪里。我是不是做错了什么?

【问题讨论】:

    标签: c++ qt qgraphicsview


    【解决方案1】:

    我能想到 2 个可能的问题:

    1. 更改视图更新模式。选项描述为here

      setViewportUpdateMode(SmartViewportUpdate);setViewportUpdateMode(FullViewportUpdate);

    2. 拖影可能是由于项目的项目边界矩形太小了半个像素。

    (我意识到这个问题很老,但也许它会帮助其他人)

    【讨论】:

      【解决方案2】:

      drawBackground 中为您提供的区域并不总是完整的视图。当场景中的某些东西发生变化时,您只能重绘相关区域。你从不同“移动”区域的左上角开始。

      解决此问题的一种方法是使初始 xy 成为 cellSize 的倍数:

      for (int x = (int)crect.left()/cellSize*cellSize; x < crect.right(); x += cellSize)
          for (int y = (int)crect.top()/cellSize*cellSize; y < crect.bottom(); y += cellSize)
      

      【讨论】:

        猜你喜欢
        • 2012-05-29
        • 2017-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-15
        • 1970-01-01
        相关资源
        最近更新 更多