Results 1 to 5 of 5

Thread: QPainter and QGraphicsView drawBackground() override

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QPainter and QGraphicsView drawBackground() override

    I have an override function for a QGraphicsView similar to what's below:

    Qt Code:
    1. void form::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. ...
    4. QBrush brush(Qt::SolidPattern);
    5. brush.setColor(Qt::black);
    6. painter->setBrush(brush);
    7. painter->drawPolygon(...);
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    I'm trying to set the entire background color of the QGraphicsView to a color, but the only way I've been able to do that is by using one of the draw functions (in the example above I used drawPolygon). Is there any functions available to do this. I tried using "setBackground(brush)" but that doesn't seem to be accomplishing what I need.

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter and QGraphicsView drawBackground() override

    Doesn't this work?

    Qt Code:
    1. void MyGV::drawBackground(QPainter *painter, const QRectF &rect){
    2. painter->save();
    3. painter->setBrush(Qt::black);
    4. painter->drawRect(rect);
    5. painter->restore();
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter and QGraphicsView drawBackground() override

    Wysota, thanks for the code example. Unfortunately it doesn't solve my problem. The issue is that I have scaling and rotating going on within the graphics view. So, if I only use the dimensions that are contained wtihin rect, I sometimes see stray lines in the graphics view when it is scaled and rotated. A quick fix was to make the rect dimensions extremely large, so that even if I zoom out and rotate it, the rect still holds a large enough dimension to paint the full background. However, I think this is a problematic solution because I am unsure of the size of the input imagery. It could be thousands of pixels, so I don't want to call a drawRect function with dimensions much larger than that. I figured if there was a way to paint the entire graphics view's background color this might solve the problem. Any idea how to do it without setting a rectangular region?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPainter and QGraphicsView drawBackground() override

    Maybe you should just enable antialiasing and smooth transformations?

  5. #5
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter and QGraphicsView drawBackground() override

    Thanks for the suggestion, but it didn't fix the prob.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.