Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: Drawing on QGraphicsView

  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,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: 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,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: 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,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: 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,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: 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,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: Drawing on QGraphicsView

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

  13. #12
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing on QGraphicsView

    What I think is you have to use QGraphicsscene, then set the graphicsscene to QGraphicsView which takes scene as agument. then add scene->addLine( yourLine ) to where you want either mouseMove, mouseRelease or mousePress.

    Once done there is no need to define any paintEvent or drawbackground.
    I used this idea to draw a line on QGraphicsView.

    Hope this helps

  14. The following user says thank you to vermarajeev for this useful post:

    JonathanForQT4 (10th April 2007)

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

    Default Re: Drawing on QGraphicsView

    //edit::thanks wysota for explaining the sequence of events

    thanks vermarajeev, this is a good quick hack I did not think about. I say hack, because there is considerable lag when it is constantly deleting/redrawing every time the mouse gets moved (meaning the user has activated the measure bar).

    The reason I wanted to get this working in paintEvent, drawForeground is because in another post I said: "since I want to draw a scale (for x and y) of the scene I have" and "The scale would look like your are looking through a rifle scope and would be over the viewport."

    Both the code above and the AxisScale (aka, rifle scope over viewport) code were in the paintEvent of the previousWidget I had made....

    Is there something easy that I am missing as to why I can't see this when I'm putting in the reimplemented paintEvent within my reimplemented QGraphicsView class?
    Can someone try this code from above in a paintEvent within a QGraphicsView and tell me it's working?

    thanks,

    Jonathan
    Last edited by JonathanForQT4; 3rd April 2007 at 14:57.

  16. #14
    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: Drawing on QGraphicsView

    Could you show us a screenshot of what you want to achieve?

  17. #15
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing on QGraphicsView

    Do you want something like this

    Qt Code:
    1. graphicView::graphicView(QGraphicsScene* s)
    2. :QGraphicsView(s), m_canvas(s){
    3. m_edgeItem = new QGraphicsLineItem( 0, m_canvas );
    4. }
    5.  
    6. void graphicView::mousePressEvent( QMouseEvent* e ){
    7. m_firstPt = e->pos();
    8. m_lastPt = e->pos();
    9. }
    10. void graphicView::mouseMoveEvent( QMouseEvent* e ){
    11. m_lastPt = e->pos();
    12. }
    13. void graphicView::mouseReleaseEvent( QMouseEvent* e ){
    14. m_lastPt = e->pos();
    15. m_edgeItem->setLine( QLineF( m_firstPt, m_lastPt ) );
    16. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Drawing on QGraphicsView

    wysota: I have attached the picture of the measuredist line as it was in my program before I started using qgraphics view (my program was a scrollarea, which you can see with a center widget (white square) plus children widgets...which are the various dots and rectangle...)

    The line in this picture is drawn within paintEvent of the central widget (white square on which dots, rectangle and the measuredist line are).

    As for "the scale would look like your are looking through a rifle scope and would be over the viewport", which I described earlier...I implemented this a little differently in reality....I kept the axis scales along the edges of the viewport as you can see.....as the user zooms in...the white center widget becomes bigger and the axis scale updates realtime...
    when the white center widget is bigger, you can also scroll (duh) and as before....the axis scale updates real time.

    vermarajeev: I pretty much did the same thing as you, the reason I say this is a hack is because of two things....the scale I use for the QGraphicsView changes when the user zooms in or out....thus the line thickness has to change in proportion to that...which is annoying, second..the there is a lag for me when I have clicked once and move the mouse around (the line is changing size to where the mouse currently is being moved in realtime). I assume this lag is because it has to draw things to the canvas real time....not exactly sure.
    The underlying point is....if I figure out how to draw it in the QGraphicsView drawForeground or paintEvent....the drawing of this should be realtime along with the axisscale (see picture above).

    I hope if have made things easier to understand and am anxious to see what you have to say.


    Thanks,

    Jonathan

    P.S. I know there are some things were fixed in 4.2.2 to 4.2.3....should I bother updating to 4.2.3?

    //edit: vermarajeev: I just implemented your code...lag still happening.... Although I update the line everytime a mouseMoveEvent occurs....(and the user has asked to measureDistance).
    Attached Images Attached Images
    Last edited by JonathanForQT4; 4th April 2007 at 12:56.

  19. #17
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing on QGraphicsView

    I have done a sample program at this link
    http://www.qtcentre.org/forum/f-qt-p...wing-6412.html

    I dont know whether it will help you. It is quite similar to what I given the example above but
    with one change. There is drawItems which might help you. I too have some issues.
    When I draw lines (straight lines in graphicsView the lines coordinates gets changed). I have put it into new thread coz it is some differnent issue.

    Hope it might help you coz as I see you want a QPainter to do stuffs and drawItem provides one.

    Anyway let me know the status
    Thanks

  20. #18
    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: Drawing on QGraphicsView

    Quote Originally Posted by JonathanForQT4 View Post
    The line in this picture is drawn within paintEvent of the central widget (white square on which dots, rectangle and the measuredist line are).
    What does the line do? Maybe you could implement it as a rubber band? If not, it's a perfect candidate for using a QGraphicsLineItem. It'll be transformed with the view, so it should be easy to handle.

    As for "the scale would look like your are looking through a rifle scope and would be over the viewport", which I described earlier...I implemented this a little differently in reality....I kept the axis scales along the edges of the viewport as you can see.....as the user zooms in...the white center widget becomes bigger and the axis scale updates realtime...
    when the white center widget is bigger, you can also scroll (duh) and as before....the axis scale updates real time.
    Nice. I would put it in drawBackground() (or drawForeground, depending if you want it drawn over or under the items).

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

    Default Re: Drawing on QGraphicsView

    vermarajeev: I have tried to help out with your problem...I hope my help is beneficial.

    wysota: the line is to measure the distance between two points. if you look at the picture again you'll see in the bottom right hand corner (in the statusbar) that the info of the line is printed there....simply put: the user can measure the distance between two points and see the line between them.

    As for the axis scale, thanks for the compliment, I too think it is rather groovy
    When I put this code in drawForeground, drawBackground etc. it does not work....this is the same with the measureDistance line (that I want to also draw within drawForeground)! I have also been trying to get both of these things to paint, but they simply don't want or do get painted.

    So, back to the code I posted from before....any clue as to why this is not working within my reimplemented drawForeground?

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

    Default Re: Drawing on QGraphicsView

    after trying lots of stuff out....drawForeground is not realtime drawing....there's always a lag been it being drawn and sometimes the line doesn't display properly and the old lines don't always get erased.

    As far as putting it in paintEvent...when I try to reimplement it my entire scene gets painted white and I don't see any objects...I'm completely baffled.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.