
Originally Posted by
Gopala Krishna
I tested with QLine but that didn't make a difference. Also i am not sure what the int cast does - i feel it just takes int part without rounding. But do you think this will make a difference since I am following one convention in all places in drawBackground() ?
It does make a difference:
{
const int gridSize = 25;
const int realLeft = static_cast<int>(std::floor(rect.left()));
const int realRight = static_cast<int>(std::ceil(rect.right()));
const int realTop = static_cast<int>(std::floor(rect.top()));
const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
const int firstLeftGridLine = realLeft - (realLeft % gridSize);
const int firstTopGridLine = realTop - (realTop % gridSize);
QVarLengthArray<QLine, 100> lines;
for (int x = firstLeftGridLine; x <= realRight; x += gridSize)
lines.
append(QLine(x, realTop, x, realBottom
));
for (int y = firstTopGridLine; y <= realBottom; y += gridSize)
lines.
append(QLine(realLeft, y, realRight, y
));
painter
->setPen
(QPen(Qt
::darkGreen,
0));
painter->drawLines(lines.data(), lines.size());
}
void drawBackground(QPainter *painter, const QRectF &rect)
{
const int gridSize = 25;
const int realLeft = static_cast<int>(std::floor(rect.left()));
const int realRight = static_cast<int>(std::ceil(rect.right()));
const int realTop = static_cast<int>(std::floor(rect.top()));
const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
const int firstLeftGridLine = realLeft - (realLeft % gridSize);
const int firstTopGridLine = realTop - (realTop % gridSize);
QVarLengthArray<QLine, 100> lines;
for (int x = firstLeftGridLine; x <= realRight; x += gridSize)
lines.append(QLine(x, realTop, x, realBottom));
for (int y = firstTopGridLine; y <= realBottom; y += gridSize)
lines.append(QLine(realLeft, y, realRight, y));
painter->setPen(QPen(Qt::darkGreen,0));
painter->drawLines(lines.data(), lines.size());
}
To copy to clipboard, switch view to plain text mode
As you see I make three changes
a) integer instead of float
b) using the containing integer rect as basis (i.e. the smallest integer rect that contains the float rect)
c) I changed the "<" in your for loops to a "<=", i thing part of your corruption was that it did not always repaint the grid when the line was at the border of the exposed rect.
The result: no more corruption of the grid...only weird color changes that I am trying to track down ;-)
EDIT: Of course you now need:
#include <cmath>
#include <cmath>
To copy to clipboard, switch view to plain text mode
for the floor, and ceil to work ;-)
Bookmarks