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. Disable background scaling for a QGraphicsView/QGraphicsScene

Disable background scaling for a QGraphicsView/QGraphicsScene

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 5.4k 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.
  • P Offline
    P Offline
    ProfessorFang
    wrote on last edited by
    #1

    Basically what I'm looking to do is replicate photoshop's transparency pattern in a QGraphicsScene, but when I scale the scene, the background scales also. I subclassed QGraphicsView and set it's background brush (as I understand it, it overrides the scene's background brush) but that didn't work either. I also tried overriding the drawBackground event in my subclassed QGraphicsView like so:
    @
    void FontPageView::drawBackground(QPainter *painter, const QRectF &rect)
    {
    painter->fillRect(rect, backgroundBrush());
    }
    @
    but that still scaled the background. Does anyone know how I could keep the background image from scaling?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      msx_br
      wrote on last edited by
      #2

      Look at:

      Examples -> Elastic Nodes Example -> graphicsview/elasticnodes/graphwidget.cpp

      In the drawBackground() the examples shows how to paint directly in the QPainter.

      So you can apply the scale factor as you want...

      msx_br - Brazil (Netherlands)

      1 Reply Last reply
      0
      • P Offline
        P Offline
        ProfessorFang
        wrote on last edited by
        #3

        [quote author="msx_br" date="1331514413"]Look at:

        Examples -> Elastic Nodes Example -> graphicsview/elasticnodes/graphwidget.cpp

        In the drawBackground() the examples shows how to paint directly in the QPainter.

        So you can apply the scale factor as you want...

        [/quote]

        Thanks, but that doesn't really help. I am already drawing directly to the QPainter of the QGraphicsView exactly like they are. Their background gradient also scales.

        Edit:
        I see now that the QPainter has a scale, so all I needed to do was keep track of the scaling manually and inverse the scaling right before drawing the background and rescale right after, like so:

        @void FontPageView::drawBackground(QPainter *painter, const QRectF &rect)
        {
        QRectF sceneRect = this->sceneRect();

        QRectF bgrects = sceneRect;
        //scale the rectangle so that it actually covers the area it's supposed to
        bgrects.setBottomRight(bgrects.bottomRight() * scalefact);
        painter->scale(1/scalefact, 1/scalefact);
        painter->fillRect(bgrects, bgbr);
        painter->scale(scalefact, scalefact);
        

        }@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          Maybe QGraphicsItem::ItemIgnoresTransformations is what you are looking for.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Arsen
            wrote on last edited by
            #5

            This code from Qt SVG viewer example disables background scaling

            void SvgView::drawBackground(QPainter *p, const QRectF &)
            {
                p->save();
                p->resetTransform();
                p->drawTiledPixmap(viewport()->rect(), backgroundBrush().texture());
                p->restore();
            }
            
            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