PDA

View Full Version : Painter drawLine doubt



arjunasd
24th August 2007, 16:01
Hi

I have use QPainter to drawlines. I draw a line based on the mouse pointer's current position. My problem is when the mouse moves the new lines are drawn but the old one's remain. Can anyone just how to remove the old lines.


I use the following command:
painter->drawLine(0, 0, x1, y1);
where x1, y1 is the current position of the mouse.

Also, when I point the mouse back to an old position where there was a line already drawn, the line disappaers. I do not understand why the old lines disappear when it should be redrawn. Could someone explain me the concept?

Thanks
Arjun

maverick_pol
24th August 2007, 17:00
Hi,

On which widget are you performing the drawing?

Maverick

arjunasd
24th August 2007, 17:50
Hi

I have a QGraphicsView and I am trying to draw the lines on the widget.

Also, I found the following piece of code while browsing. Its in Qt3.
They have a QScrollView and they paint the lines as follows:



painter->drawLine(x1,y1, x2, y2);

change the values of x's and y's

painter->drawLine(x1,y1, x2, y2);



They comment the 1st draw clears the previous line and second draws the new line. How does this work? Sorry if its a naive question?

Arjun

jpn
24th August 2007, 18:56
QGraphicsView caches its content. It is not suggested to draw by hand on GV. Perhaps you could use QGraphicsLineItems intead?

arjunasd
24th August 2007, 19:10
Thanks. It works fine with QGraphicsLineItem.

Just out of curiosity, regarding the sample code I got from the internet. Its in Qt3.They have a QScrollView and they paint the lines as follows:

painter->drawLine(x1,y1, x2, y2);
change the values of x's and y's
painter->drawLine(x1,y1, x2, y2);


They comment the 1st draw clears the previous line and second draws the new line. How does this work?

Thanks
Arjun

jpn
24th August 2007, 19:59
How does this work?
Hard to say without seeing the exact code. Maybe they switch colors in between?

Anyway, the painting system has pretty much changed in Qt 4. One can no more paint outside QWidget::paintEvent(). For more details, see Porting to Qt 4 - Painting and Redrawing Widgets (http://doc.trolltech.com/4.3/porting4.html#painting-and-redrawing-widgets) and The Arthur Paint System (http://doc.trolltech.com/4.3/qt4-arthur.html).