Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. GraphicsView background that independs from zoom and scrolling
Qt 6.11 is out! See what's new in the release blog

GraphicsView background that independs from zoom and scrolling

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 6.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IrQX
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IrQX
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • I Offline
        I Offline
        IrQX
        wrote on last edited by
        #3

        up

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

          question, how are you drawing the grid lines?

          1 Reply Last reply
          0
          • I Offline
            I Offline
            IrQX
            wrote on last edited by
            #5

            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);@

            1 Reply Last reply
            0
            • I Offline
              I Offline
              IrQX
              wrote on last edited by
              #6

              solution was simple: add @resetCachedContent();@ to @void scrollContentsBy(int dx, int dy);@ function.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kamalakshantv
                wrote on last edited by
                #7

                test

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  cool @ IrQX

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    IrQX
                    wrote on last edited by
                    #9

                    no pleasure

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved