PDA

View Full Version : Revisiting drawForeground



JonathanForQT4
23rd April 2007, 08:42
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 :(

marcel
23rd April 2007, 09:20
Why don't you use QGraphicsView::mapFromScene( const QPointF& )?
It maps from scene coordinates to view coordinates.

Regards

JonathanForQT4
23rd April 2007, 09:42
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

aamer4yu
23rd April 2007, 09:54
what are u tryng to draw in the foreground ??
As marcel said, mapFromScene should work... can we have some sample code ??

JonathanForQT4
23rd April 2007, 10:04
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();

}

aamer4yu
23rd April 2007, 10:10
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)

JonathanForQT4
23rd April 2007, 10:25
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...

JonathanForQT4
24th April 2007, 08:35
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

mr.costa
24th April 2007, 17:58
I think you may subclass viewport



void MyView::paintEvent( QPaintEvent *e) {
QPainter p( this );
p.draw.....
.....
QGraphicsView::paintEvent( e );
}

JonathanForQT4
25th April 2007, 08:55
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?

mr.costa
25th April 2007, 16:29
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::paintEvent().

JonathanForQT4
26th April 2007, 08:35
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