void window::paintEvent(QPaintEvent *pe){ QPainter paint(this); paint.setPen(Qt::blue); paint.drawText(rect(), Qt::AlignCenter, "The Text");}
bool DrawEventCatcher::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::Paint) { QPainter paint(this); paint.setPen(Qt::blue); paint.drawLine(0,0,100,100); return true; } else { // standard event processing return QObject::eventFilter(obj, event); } }
error C2664: 'QPainter::QPainter(QPaintDevice *)' : cannot convert parameter 1 from 'DrawEventCatcher *const ' to 'QPaintDevice *'
class DrawEventCatcher: public QObject{ Q_OBJECTprotected: bool eventFilter(QObject *obj,QEvent *event);};
bool DrawEventCatcher::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::Paint) { QWidget* pWidget = dynamic_cast<QWidget*>(obj); //Можно еще таким образом написать верхнюю строчку //QWidget* pWidget = qobject_cast<QWidget*>(obj); //Но мне больше импонирует первый вариант if (NULL != pWidget) { QPainter paint(this); paint.setPen(Qt::blue); paint.drawLine(0,0,100,100); return true; } else return false; } else { // standard event processing return QObject::eventFilter(obj, event); } }