{
Q_OBJECT
public:
{
}
protected:
{
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;
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;
}
};
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;
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks