Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29

Thread: Drawing on QGraphicsView

  1. #21
    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

    I would make the line a regular item. I would create it when mouse button is pressed and destroy when it is released. As you'll have the line coordinates in scene coordinate system, calculating the distance regardless of the scale, etc. should be easy.

  2. #22
    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

    Here is an example of what I mean.
    Attached Files Attached Files

  3. #23
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Drawing on QGraphicsView

    Quote Originally Posted by wysota View Post
    Here is an example of what I mean.
    Do you think an update() call is needed in each of the mouse events implemented in the reimplemented scene class? I tried commenting them and it worked fine.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  4. #24
    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

    It doesn't slow anything down, so why not? But you're right, it's not required.

  5. #25
    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

    Thanks to wysota for his quick solution.
    I made a mistake not to override QGraphicsScene. Anyway I'll try getting the solution overriding QGraphicsView and without overriding QGraphicsScene and see how it works.

    JonathanForQT4-->
    It was nice to see your quick solution. Thanks!!! wysota made my work easy.

    Njoy and have fun as I'm with qt4.

    //And it works... Anything is possible with qt4
    Last edited by vermarajeev; 7th April 2007 at 11:30.

  6. #26
    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: thanks for your code, it is pretty much the same thing I had already implemented after vermarajeev suggested this
    The reason I keep posting is because I wanted to find out how to get it to work within drawForeground so that I could get the axis scale painting within there too....Or would you also suggest making the axis scale some items -- I think things would get too sluggish.

    I was also thinking that you could add a widget to the scene....and the widget could be the axis scale....any ideas if this is doable?

    I was reading this:

    http://labs.trolltech.com/blogs/2007...on-the-canvas/

    and this:

    http://labs.trolltech.com/blogs/2007...s-and-widgets/

    and that's why I ask if I should put my axis scale in a widget's paintEvent (like it was before in the picture I attached) and then add the widget overtop of the viewPort from QGraphicsView.

    The whole thing seems stupid to me though because QGraphicsView is a inherits QWidget and I should be able to paint over the viewPort, arggggg!

    vermarajeev: I have now added thanks for this first suggestion of making the measureDist line an QGraphicsItem

  7. #27
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing on QGraphicsView

    I am also having the task of drawing an axis scale on a QGraphicsView, that is supposed to be at a fixed widget position, with fixed linewidth etc. I think I finally found a solution. Basically, you can just override drawForeground, turn off the world coordinate system mapping and draw directly in view coordinates. The following code would for example draw the axes as two plain lines:

    Qt Code:
    1. void myGraphicsView::drawForeground(QPainter *painter, const QRectF &rect)
    2. {
    3. int axesOffset = 10;
    4. int bottom = height() - 2*axesOffset;
    5. int right = width() - 2*axesOffset;
    6.  
    7. painter->setWorldMatrixEnabled(false);
    8. painter->setPen(Qt::white);
    9. painter->drawLine(axesOffset,axesOffset,axesOffset,bottom);
    10. painter->drawLine(axesOffset,bottom,right,bottom);
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to hb for this useful post:

    JonathanForQT4 (8th June 2007)

  9. #28
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: Drawing on QGraphicsView

    No drawing is "real-time" drawing in Qt. Not on X11, not Windows, not OpenGL. There's always an asynchronous mechanism; anything else is bound to cause flicker, or be very inefficient at the very least.

    If you want to understand how QGraphicsView::drawBackground and ::drawForeground work, you should create yourself a very small simple example with a 100x100 scene in a plain view, and see what happens if you draw shapes of various colors at different places. Start by drawing the scene's scene rect, filling it with a solid color, and see what happens when you resize the view, and move the scrollbars.

    Qt Code:
    1. void CustomView::drawBackground(QPainter *painter, const QRectF &exposed)
    2. {
    3. painter->fillRect(sceneRect(), Qt::blue);
    4. }
    5.  
    6. void CustomView::drawForeground(QPainter *painter, const QRectF &exposed)
    7. {
    8. // note: semi-transparent color, otherwise the items and
    9. // background won't shine through the foreground
    10. painter->fillRect(sceneRect(), QColor(64, 128, 192, 128));
    11. }
    To copy to clipboard, switch view to plain text mode 

    Don't expect drawing to be "instantaneous"; that's not how Qt works. :-) Still, you can easily get frame rates beyond 100fps if you do things right.

    Btw, I also recommend http://doc.trolltech.com/4.2/graphic...w-architecture as bed time reading, and if you look at the examples (elastic-nodes uses drawBackground, for example), you'll probably get the picture quite soon :-). Notice especially the part that explains the coordinate systems; since drawForeground and drawBackground render in scene coordinates, but QWidget:aintEvent() renders in device coordinates.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

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

    Default Re: Drawing on QGraphicsView

    thanks hb I was able to use the painter->setWorldMatrixEnabled(false); trick and can now re use my code from my old implementation of this problem

    Cheers,
    Jonathan

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.