Results 1 to 12 of 12

Thread: Revisiting drawForeground

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

    Default Revisiting drawForeground

    Ok, so I've changed a lot with my program in the last little while and now I'm back at this stage....

    I have stuff drawing in drawForeground of my QGraphicsView implementation, but everything seems to be in QGraphicsScene coordinates...all relative to the scene....which isn't very helpful to me....I was wondering if there was a way to set it to the coordinates of the viewport?....I'm sure it's a function I overlooked, but thought I'd ask while I look some more...

    Thanks

    Jonathan

    edit://going to fool around with initFrom...looks promising

    ok, I just tried, painter->initFrom(this->viewport()); in my QGraphicsView....still not working...

    when I draw from 0 to viewport()->height() it still draws a line from 0 on the scene to the length of the viewports height
    Last edited by JonathanForQT4; 23rd April 2007 at 09:01.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Revisiting drawForeground

    Why don't you use QGraphicsView::mapFromScene( const QPointF& )?
    It maps from scene coordinates to view coordinates.

    Regards

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

    Default Re: Revisiting drawForeground

    well since what I'm drawing has to always be in the viewport (to see) I have used mapToScene and just put in the viewport coordinates...as of right now...this is not working..because everytime I move the scrollarea scroll bars around, it doesn't always get repainted....weird!

    which was the same problem I was having in my last thread...

    edit://here's a pic of my problem
    Attached Images Attached Images
    Last edited by JonathanForQT4; 23rd April 2007 at 09:46.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Revisiting drawForeground

    what are u tryng to draw in the foreground ??
    As marcel said, mapFromScene should work... can we have some sample code ??

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

    Default Re: Revisiting drawForeground

    what I'm trying to do is an axis scale...and each time the user moves the scroll bars (to navigate around the scene) the scale updates....I have thought about adding these lines to the scene itself.....like I have done with my last problem (measure distance between two points)...but I it would be laggy...

    So I'm doing it in drawForeground before when I do something I implement paintEvent the entire viewPort goes white (no clue why..)

    Here's my code from drawForeground...note the scale is (1, -1) because the entire scene itself is flipped....

    void instanceOfQGraphicsView::drawForeground(QPainter *painter, const QRectF &rect){
    painter->initFrom(this->viewport());
    painter->save();
    painter->scale(1, -1);
    QPen tmp;
    tmp.setColor(QColor(230, 100, 50, 200));
    tmp.setWidth(3);
    painter->setPen(tmp);

    QPoint origin(2, 2);
    QPoint vertical(2, this->viewport()->height()-5);
    QPoint horizontal(this->viewport()->width()-5, 2);
    painter->drawLine(mapToScene(origin), mapToScene(vertical));
    painter->drawLine(mapToScene(origin), mapToScene(horizontal));

    painter->restore();

    }
    Attached Images Attached Images

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Revisiting drawForeground

    Why dont u try implementing them as lines ??
    I dont think ther shud be much problem....it wud be rather easy for u maintain transformations (mapping from scene to viewport , vice versa)

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

    Default Re: Revisiting drawForeground

    because it seems useless to add more items to the scene when what needs to be drawn should only ever be in the viewport....
    but alas, if this is the only way to do this...I will have to do it this way...

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

    Default Re: Revisiting drawForeground

    I have an idea and I thought I'd bounce it off you guys....

    what if I was to make a widget that is the size of the viewport...and then I place is overtop of the viewport somehow...and draw everything on that widget...

    the problems I foresee with this is....if I want to print out the what's seen on the screen, I have to somehow merge this new widget and the viewport. How do I put the widget directly overtop of the viewport?

    what does everyone think?

    thanks,

    Jonathan

  9. #9
    Join Date
    Mar 2006
    Posts
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Revisiting drawForeground

    I think you may subclass viewport

    Qt Code:
    1. void MyView::paintEvent( QPaintEvent *e) {
    2. QPainter p( this );
    3. p.draw.....
    4. .....
    5. QGraphicsView::paintEvent( e );
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Revisiting drawForeground

    mr.costa, I am not quite sure what you mean by this...MyView is now a implementation of widget?
    and then I call the paint event within my QGraphicsView implementation?

  11. #11
    Join Date
    Mar 2006
    Posts
    7
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Revisiting drawForeground

    MyView inherits QGraphicsView. The paintEvent is called by Qt internals. User may call update() (slot) to schedule the paint in the next refresh round. So you can call update() several times before the paint ocurrs, preventing CPU fog.

    With this method, you paint the widget before anything, I do not know if is in widget abosolute coordinates, but it is a nice try. Remeber to look QPaintEvent::rect(). Oh! And finish() the QPainter before QGraphicsView:aintEvent().

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

    Default Re: Revisiting drawForeground

    mr.costa:

    I figured out later on yesterday what you meant...and tried it, albeit that I didn't use QPaintEvent::rect() or finish()...I just tried with those two things....and neither work when I call them within QPaintEvent. Is finish() supposed to be end()?

    thanks

    Jonathan

Similar Threads

  1. implementing drawForeGround
    By Wirloff in forum Newbie
    Replies: 9
    Last Post: 11th April 2007, 16:03

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.