GraphicsView background that independs from zoom and scrolling
-
Hello!
I writing application and need to paint static background like this: !http://img5.imageshack.us/img5/3594/62930782.png(original.)!
code for this is: @void MainView::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->save();
QLinearGradient backgroundGradient(rect.left(),rect.top(),rect.left(),rect.bottom());
backgroundGradient.setColorAt(0, QColor(170, 185, 220));
backgroundGradient.setColorAt(1, QColor(240, 240, 250));
painter->setPen(QPen(Qt::NoPen));
painter->setBrush(QBrush(backgroundGradient));
painter->drawRect(rect);
painter->restore();
}@So, background not moves and independs from zooming: !http://img836.imageshack.us/img836/8269/65737204.png(zoom)!
But I have problem if I try to scroll it: !http://img255.imageshack.us/img255/2339/38345441.png(scroll)!
I know that the reason of this bug is variable rect, that recieves a small rectangle value on update event after scrolling. And I ask you: what is the best way to prevent this bug?
Thank you.
-
BTW, in 1st and 2nd pictures background the same (start-color at top and stop-color at bottom)
What coordinates I should use instead of rect variable?
-
up
-
question, how are you drawing the grid lines?
-
Grid lines:
@const int GridSize = 40;
QPen pen(QColor(175, 175, 175, 127));
m_gridGroup = new QGraphicsItemGroup;
const int MaxX = static_cast<int>(std::ceil(m_scene->width()) / GridSize) * GridSize;
const int MaxY = static_cast<int>(std::ceil(m_scene->height()) / GridSize) * GridSize;
for (int x = 0; x <= MaxX; x += GridSize)
{
QGraphicsLineItem *item = new QGraphicsLineItem(x, 0, x, MaxY);
item->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
item->setPen(pen);
item->setZValue(-1000);
m_gridGroup->addToGroup(item);
}
for (int y = 0; y <= MaxY; y += GridSize)
{
QGraphicsLineItem *item = new QGraphicsLineItem(0, y, MaxX, y);
item->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
item->setPen(pen);
item->setZValue(-1000);
m_gridGroup->addToGroup(item);
}
m_gridGroup->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
m_scene->addItem(m_gridGroup);@ -
solution was simple: add @resetCachedContent();@ to @void scrollContentsBy(int dx, int dy);@ function.
-
test
-
cool @ IrQX
-
no pleasure