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. QGraphicsView background color different from QGraphicsScene
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView background color different from QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsviewqgraphicsscenebackground
6 Posts 3 Posters 2.2k Views
  • 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.
  • W Offline
    W Offline
    wazzdaman
    wrote on last edited by
    #1

    Hi,

    I'd like to paint the background of QGraphicsView in a different color than I'm using in QGraphicsScene::drawBackground(). I use drawBackground() for some custom drawing in sceneRect() so using setBackgroundBrush() is out of question.

    How can I do this? Any tips?

    Thanks a lot!

    JonBJ JoeCFDJ 3 Replies Last reply
    0
    • W wazzdaman

      Hi,

      I'd like to paint the background of QGraphicsView in a different color than I'm using in QGraphicsScene::drawBackground(). I use drawBackground() for some custom drawing in sceneRect() so using setBackgroundBrush() is out of question.

      How can I do this? Any tips?

      Thanks a lot!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @wazzdaman
      There is void QGraphicsView::drawBackground(QPainter *painter, const QRectF &rect). It's virtual protected.

      When you say " I use drawBackground() ..." are you talking about on QGraphicsScene or QGraphicsView? View defaults to scene, but you override view one if you want different background.

      1 Reply Last reply
      0
      • W wazzdaman

        Hi,

        I'd like to paint the background of QGraphicsView in a different color than I'm using in QGraphicsScene::drawBackground(). I use drawBackground() for some custom drawing in sceneRect() so using setBackgroundBrush() is out of question.

        How can I do this? Any tips?

        Thanks a lot!

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3

        @wazzdaman Try to add an openglwidget to the viewport of QGraphicsView and set different background color to it? I did not try it.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wazzdaman
          wrote on last edited by
          #4

          @JoeCFD @JonB

          Thanks for your comments! I override QGraphicsScene::drawBackground() for my purposes.

          It turned out it's easier to solve this issue than I thought. I just changed my code in drawBackground() to this code and it worked perfectly:

          painter->fillRect(rect, QBrush(Qt::lightGray));
          painter->setClipRect(sceneRect() & rect);
          painter->fillRect(sceneRect(), Qt::white);
          

          Is there any simpler way to do this?

          1 Reply Last reply
          0
          • W wazzdaman

            Hi,

            I'd like to paint the background of QGraphicsView in a different color than I'm using in QGraphicsScene::drawBackground(). I use drawBackground() for some custom drawing in sceneRect() so using setBackgroundBrush() is out of question.

            How can I do this? Any tips?

            Thanks a lot!

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @wazzdaman said in QGraphicsView background color different from QGraphicsScene:

            I'd like to paint the background of QGraphicsView in a different color

            Since you wrote this I would expect you to be overriding QGraphicsView::drawBackground() for this, rather than QGraphicsView::drawScene(), regardless of what you might be doing in QGraphicsScene::drawBackground().

            W 1 Reply Last reply
            2
            • JonBJ JonB

              @wazzdaman said in QGraphicsView background color different from QGraphicsScene:

              I'd like to paint the background of QGraphicsView in a different color

              Since you wrote this I would expect you to be overriding QGraphicsView::drawBackground() for this, rather than QGraphicsView::drawScene(), regardless of what you might be doing in QGraphicsScene::drawBackground().

              W Offline
              W Offline
              wazzdaman
              wrote on last edited by
              #6

              @JonB I moved my code into QGraphicsView::drawBackground() as you recommended, it works nicely. Thanks a lot!

              1 Reply Last reply
              1
              • W wazzdaman has marked this topic as solved on

              • Login

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