Results 1 to 20 of 29

Thread: Drawing on QGraphicsView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Drawing on QGraphicsView

    Hey guys, I'm trying to draw some stuff within the viewport of QGraphicsView....when I do this within paintEvent it doesn't work for some reason.

    Is the QGraphicsScene for the view overwriting what I am drawing (..although my scene's color is transparent)? Any help ideas would be appreciated.

    Thanks,
    Jonathan

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

    Default Re: Drawing on QGraphicsView

    In general you shouldn't draw on the graphics view directly from within the paint event. Instead use drawBackground() and drawForeground() methods.

  3. The following user says thank you to wysota for this useful post:

    tprabhuit (30th March 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing on QGraphicsView

    I just wrote drawForeground, since I want to draw a scale (for x and y) of the scene I have, I called drawForeground from paintEvent since I need it updated whenever I call update(). The painter I pass it is QPainter mainPainter(this); (from paintEvent) and the Rect I pass it is the rect I defined with setSceneRect....since coordinates are relative to the scene.

    The scale would look like your are looking through a rifle scope and would be over the viewport. I got it working when I had defined a widget (everything written in the paintEvent) within a qscrollarea....but now I'm using QGraphicsView and am having trouble with drawing whatsoever.

    thanks again,

    Jonathan

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

    Default Re: Drawing on QGraphicsView

    But why do you reimplement the paint event in the first place? You should reimplement drawForeground or drawBackground instead.

  6. #5
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing on QGraphicsView

    I did reimplement drawForeground...but it's not painting anything within the viewport....

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

    Default Re: Drawing on QGraphicsView

    How did you reimplement it? Can we see the code? Did you take into consideration that all coordinates are in the scene coordinate system?

  8. #7
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing on QGraphicsView

    Qt Code:
    1. void someclass::drawForeground(QPainter *painter, const QRectF &rect){
    2.  
    3. if(m_measureDistDragging){
    4. QPen tmp;
    5. tmp.setColor(QColor(230, 100, 50, 200));
    6. tmp.setWidth(1);
    7. painter->setPen(tmp);
    8. painter->drawLine(m_measureDistStartPoint, m_currentMousePos);
    9. }
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    when the mouse is clicked m_measureDistStartPoint is set to mapToScene(event->pos()) since when I use it for drawing it needs to be in scene coordinates. The same applies to m_currentMousePos.

    When I am debugging I can see that the line is there, but when I am just using the program, I never see the line drawn. Any ideas?

    //edit: btw. m_measureDistDragging is true....
    Last edited by wysota; 28th March 2007 at 09:52.

  9. #8
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing on QGraphicsView

    anyone have any ideas? I can see the line drawn when I am debugging and see the line right after this code is executed and the window pops up....I'm guessing it's overwritten in paintEvent or something?

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

    Default Re: Drawing on QGraphicsView

    Did you override the paint event?

  11. #10
    Join Date
    Jan 2007
    Posts
    81
    Thanks
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing on QGraphicsView

    I tried the reimplementing paintEvent with the same code (the class was just a widget before and it worked then...no it's a QGraphicsView class....but should still work since QGraphicsView inherits QWidget....), but to no avail...then you said, "But why do you reimplement the paint event in the first place? You should reimplement drawForeground or drawBackground instead."
    So...I tried foreground....I'm now assuming that paintEvent is drawing over it?

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

    Default Re: Drawing on QGraphicsView

    No. paintEvent calls drawForeground() after calling drawBackground() and drawItems(). It doesn't do anything after drawForeground.

Similar Threads

  1. [SOLVED] QTreeView drawing selection with icons
    By Timewarp in forum Qt Programming
    Replies: 7
    Last Post: 7th February 2013, 07:52
  2. Drawing the background of QGraphicsView
    By aknuds1 in forum Qt Programming
    Replies: 13
    Last Post: 9th March 2007, 14:53
  3. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  4. Drawing on QWidget - strech & resize
    By kemp in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2007, 14:39
  5. Regarding QGraphicsView
    By kiranraj in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2006, 04:59

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.