PDA

View Full Version : how to get QPainter object for QLabel



bpatel
19th September 2009, 18:52
i have a widget and implemented a QpainetEvent;
inside that i write
{
Qpainter painter(this);
}

here i have a QLabel widget on my main widget
i want QPainter object of that QLabel insted of main widget
like
"QPainter painter(this)" instead of that statement can i write this statement "QPainter painter(label1)"

wysota
20th September 2009, 00:59
You can't. You can only paint on a widget from within its own paint event. If you want to capture paint event of another widget, please install an event filter for it.

bpatel
20th September 2009, 05:05
ok thanks for reply but your reply confused me more
because i m totally new in qt so can u little bit tell me how to write event on that widget.......
or some short code so i can get some idea.......

wysota
20th September 2009, 06:57
Open up Qt Assistant and type in "event filter" in the index tab.

bpatel
20th September 2009, 07:28
thanks........

suneyes
15th March 2010, 07:39
If your not subclassing QLabel, and instead only using QLabel, then use the following :



bool myView::eventFilter(QObject *obj, QEvent *eve)
{
switch(eve->type())
{
case QEvent::Paint:
{
return handlePaintEvent(obj, eve);
}
default:
{
return QObject::eventFilter(obj, eve);
}
}
} // eventFilter

bool myView::handlePaintEvent(QObject *obj, QEvent *eve)
{
QPainter painter(static_cast<QWidget *>(obj)); //here u may check if obj is your ui.frame()
......
}


But if you are subclassing QLabel, better override its paintEvent() and initialize a painter like :


void myLabel::paintEvent ( QPaintEvent * event )
{
QPainter painter(this);
.......
}

aamer4yu
15th March 2010, 09:52
In my opinion it would be simple and better to go for subclassing the QLabel and doing your drawing in the paintEvent() function.
Something as suggested above -

void myLabel::paintEvent ( QPaintEvent * event )
{
QPainter painter(this);
....... // do your drawing..
QLabel::paintEvent(event); // Pass to base class if needed.
}

Fook42
28th May 2010, 10:30
.. maybe this is a dirty way .. but it works for me:



QPixmap *pix;
pix=(QPixmap *)label->pixmap();
QPainter label_painter(pix);


then you may use "label_painter" for your needs.