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
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    17
    Thanked 3 Times in 3 Posts

    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.

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

    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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).

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

    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?

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

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    Default Re: Drawing on QGraphicsView

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

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

    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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    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.

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

    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.

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

    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

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

    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 

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

    JonathanForQT4 (8th June 2007)

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

    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

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

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