PDA

View Full Version : How to draw lines above a frame with background image



athulms
19th August 2011, 04:47
I am newto Qt so i want to know about how to draw lines above a frame with background image with Qpainter. when i use painter(this) it draw over the main window which is hidden by the frame with background images. how can i overcome that. when i use painter(ui->frame) it doesnot work. pls help.

Santosh Reddy
19th August 2011, 05:03
Looks like you are trying to paint on a QFrame from outside the QFrame::paintEvent(), this is not a good way to do, but may work (depends on from where and how you paint)

when i use painter(ui->frame) it doesnot work. pls help.
This should work, what you you mean by does not work, does it draw at all, where does it draw? also from where you paint. (as it look you are not painting from QFrame::paintEvent())

stampede
19th August 2011, 07:13
This is third thread started on the same issue, apparently full working code (posted here (http://www.qtcentre.org/threads/43973-how-to-draw-a-line-over-a-frame)) is not enough for you. I guess anything written here goes to /dev/null anyway ...

athulms
19th August 2011, 09:02
void mainwindow::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setCapStyle(Qt::RoundCap);
linepen.setWidth(30);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
if(y==1)
painter.drawPoint(point);
else
painter.drawLine(point,point2);

}

I used this code. for painter(this) i tryed painter(ui->frame3).
( i have 3frames for equal size, i use..

ui->frame1->hide();ui->frame2->hide();ui->frame3->show();
to show each frame using button click.
I have to draw only on the third frame. This code draws on the mainwindow ui window

stampede
19th August 2011, 09:13
RTFM.

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.
If you want to use QPainter on a widget, you can do that ONLY from it's paintEvent. This implies the following - if you want to draw something on a widget, you have to create a subclass. End of story. You can't draw on a widget from some other widget paintEvent, and this is what you are trying to do the whole time.