我正在尝试在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();
}
问题可以在这里看到:
当我移动该项目时,它会在其后面留下一串网格点,这些网格点未与原始网格对齐。
我不明白这个错误从何而来。我是不是做错了什么?
最佳答案
我可以想到两个可能的问题:
更改 View 更新模式。这些选项描述为 here .
setViewportUpdateMode(SmartViewportUpdate);
或setViewportUpdateMode(FullViewportUpdate);
拖尾可能是由于项目的项目边界矩形太小半个像素造成的。
(我意识到这个问题很老了,但也许对其他人有帮助)
关于c++ - QGraphicsView::drawBackground 中的故障网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27623953/