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

QGraphicsView background color different from QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsviewqgraphicsscenebackground
6 Posts 3 Posters 1.9k 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 17 Feb 2023, 16:25 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!

    J J 3 Replies Last reply 17 Feb 2023, 18:05
    0
    • W wazzdaman
      17 Feb 2023, 16:25

      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!

      J Offline
      J Offline
      JonB
      wrote on 17 Feb 2023, 18:05 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
        17 Feb 2023, 16:25

        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!

        J Offline
        J Offline
        JoeCFD
        wrote on 17 Feb 2023, 18:42 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 18 Feb 2023, 15:36 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
            17 Feb 2023, 16:25

            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!

            J Offline
            J Offline
            JonB
            wrote on 18 Feb 2023, 15:52 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 21 Feb 2023, 12:48
            2
            • J JonB
              18 Feb 2023, 15:52

              @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 21 Feb 2023, 12:48 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 22 Feb 2023, 11:40

              5/6

              18 Feb 2023, 15:52

              • Login

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