Results 1 to 4 of 4

Thread: QGraphicsView advantages of drawBackground and drawForeground?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsView advantages of drawBackground and drawForeground?

    So, for example, if I had another function that was responsible for drawing a diagonal line across the widget, I could do something along the lines of
    No. You cannot do any drawing to the screen in Qt outside of the paintEvent() (or a function called while inside paintEvent()) of the QWidget in which you want to do the drawing (unless you have a custom QPaintEngine, which you will likely never have).

    Qt Code:
    1. QPainter(viewport());
    To copy to clipboard, switch view to plain text mode 
    There is no such QPainter constructor that simply takes a rect as an argument. QPainter always renders its output to a QPaintDevice instance you give it as a constructor argument, and the only place you can construct a QPainter to render to the screen is inside of the QWidget's paintEvent().

    You are never dependent on drawForeground() or drawBackground() if all you are drawing to the screen are QGraphicsItems added to a QGraphicsScene. These two functions exist only to provide you a "hook" into QGraphicView's paintEvent() processing so that you can draw things before the QGraphicsScene is rendered (drawBackground()) or after the scene is rendered (drawForeground()). The widget is a sandwich - background bread on the bottom, scene meat in the middle, and foreground bread on the top. If you just have meat, then you can omit the bread.

    Everything (background, scene, forground) is redrawn with every paintEvent(). The QPaintEvent::rect() and QPaintEvent::region() methods give you the part of the widget that needs to be repainted so you don't have to repaint the entire thing if your painting is complex. In most cases, most widgets repaint everything all the time because their painting needs are simple.

    Qt Code:
    1. a.drawLine(0,0,viewport()->rect().x(),viewport()->rect().y());
    To copy to clipboard, switch view to plain text mode 

    Even if you could do this, this code would not give you a diagonal line, it would give you a dot in the top left corner. (0, 0) is top left, and x() and y() return the left (0) and top (0) coordinates, respectively.
    Last edited by d_stranz; 25th July 2017 at 23:40.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. The following user says thank you to d_stranz for this useful post:

    Bla1 (26th July 2017)

Similar Threads

  1. What are the advantages of developing in Qt this 2017?
    By laurantx0ne in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2017, 21:34
  2. QGraphicsView\Scene drawBackground bitmap as geomap
    By pethead in forum Qt Programming
    Replies: 1
    Last Post: 29th October 2010, 13:45
  3. Advantages of commercial Qt other than licensing rights.
    By babu198649 in forum General Discussion
    Replies: 6
    Last Post: 23rd September 2008, 13:51
  4. QPainter and QGraphicsView drawBackground() override
    By forrestfsu in forum Qt Programming
    Replies: 4
    Last Post: 5th December 2006, 15:52

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.