【发布时间】: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