PDA

View Full Version : overloading paintEvent() function



Aayush
19th July 2011, 05:22
Hello All!!

I was wondering if there is any way we could overload the paintEvent function with more parameters so that I could pass on other informations to the painEvent. And if yes, I guess there should be some kind of way to explicitly call the overloaded function as well.

Actually, I want to draw on a particular QLabel inside my QMainWindow. I could've made a sub class of QLabel bt I have come a long way without doing so.... So, to do it with a subclass, I'd have to start over and it is a lot of work and I dont have much time til the deadline.

So, can you please help me on this??

Regards, Aayush Shrestha

Santosh Reddy
19th July 2011, 06:04
BTW, what extra information you want to pass to paintEvent? If you have not implemented the paintEvent function, what is the use of sending other information, it will just be ignored by the standard QLabel paintEvent implementation, think again what you wan to do.

Any way, one alternate for sub-classing QLabel, is to install a event filter on that particular instance on QLabel, and interrupt all the paint events, and do your custom functions instead.

I hope you have explored all the options of using the standard QLabel as is :)

Aayush
19th July 2011, 13:40
BTW, what extra information you want to pass to paintEvent? If you have not implemented the paintEvent function, what is the use of sending other information, it will just be ignored by the standard QLabel paintEvent implementation, think again what you wan to do.

Any way, one alternate for sub-classing QLabel, is to install a event filter on that particular instance on QLabel, and interrupt all the paint events, and do your custom functions instead.

I hope you have explored all the options of using the standard QLabel as is :)

I have installed event filter onto my imageLabel (a QLabel). I use the event filter to detect mouse press, mouse release and mouse move. After detecting, I draw a line on the image that is currently on the Label by calculating mouse position. Now, I have to paint the modified image on the Label. My idea was to do so in the paintEvent function. Now, I have to send the modified image to the paintEvent function so that it could be painted over the label.

What can I do for that??

Please Help!!

nish
19th July 2011, 14:33
just use QLabel::setPixmap(yourModifiedPixmap);

Santosh Reddy
20th July 2011, 05:57
You can directly draw on the widget (QLabel in your case).

I can give a an example, here I have QWidget (or any other widget), which I don't want to sub-class, or it is already being used heavily in design. Then I will create a event filer (say CustomPaint), and install it on objects which I want and do my custom painting instead of default widget painting.


class CustomPaint : public QObject
{
Q_OBJECT
public:
CustomPaint(QObject *parent) :
QObject(parent)
{
}

protected:
bool eventFilter(QObject * object, QEvent * event)
{
if(event->type() == QEvent::Paint)
{
QWidget * widget = dynamic_cast<QWidget *>(object);
if(widget)
{
//[1]. Call this if you want to have original widget painting in background, else comment it out
object->event(event); //Note: this will call QObject::event(), you cannot directly call QWidget::event()

//[2]. Sample paint, draws a rounded rectangle, as per original widget's size hint
{
QPainter painter(widget); //Note: widget is parent

int width = 8;
QPen pen;
pen.setWidth(width);

painter.setPen(pen);
QRect rect = QRect(width/2, width/2, widget->sizeHint().width() - width, widget->sizeHint().height() - width);

painter.setRenderHint(QPainter::Antialiasing);
painter.drawRoundedRect(rect, 20, 20);
}
//[3]. return false; if you want to have the original widget also to be painted
return true; //filter the event, original widget will not paint
}
}
return false;
}
};


Just install this on the widget you want

Following can be use cases for this example
1. If a widget does not have default paint, you can implement a paint, with out sub-classing
2. If you want a widget's painting to be done on some background, then you can do it, with out sub-classing (three are simple ways to do so)
3. If you want to paint extra content on a widgets content, with out sub-classing

combinations of these, or any innovative way.

Aayush
20th July 2011, 20:59
Thanks... :)