wysota
5th March 2008, 20:22
Hi people. I just came up with an idea I consider worthy of adding to Qt and I'm going to submit it as a suggestion to Trolltech. But before I do, I'd like to know your opinions on it and possible usecases of the solution. Please take time to think about it and write your ideas and comments in this thread. Of course I'm open for criticism :cool:
The idea is to have something similar to eventFilter() and friends but different in that way that it is executed after the event has been delivered (contrary to eventFilter that is executed before the event has been delivered). My example usecase is demonstrated with the code below and the screenshot attached.
Also attached you'll find a file containing a macro that emulates "post event filters" on "user" level for single widget classes. To use it just include it in your code, make a widget subclass and add a WW_POSTEVENTFILTER macro just below the Q_OBJECT macro. When implementing postEventFilter() remember that it has to be a public slot (I'm using invokeMethod() to call the method without knowing its class name).
Here is an example:
#include "posteventfilter.h"
class PushButton : public QPushButton {
Q_OBJECT
WW_POSTEVENTFILTER(QPushButton)
public:
PushButton(QWidget *parent=0) : QPushButton(parent){}
};
class Dummy : public QObject {
Q_OBJECT
public:
Dummy(bool tl, QObject *parent=0) : QObject(parent){ m_tl = tl;}
public slots:
virtual bool postEventFilter(QObject *o, QEvent *e){
if(e->type()==QEvent::Paint){
QWidget *w = (QWidget*)o;
QPainter p(w);
QPen pe = p.pen();
pe.setColor(m_tl ? Qt::red : Qt::blue );
pe.setWidth(3);
p.setPen(pe);
p.setRenderHint(QPainter::Antialiasing);
if(m_tl){
p.drawLine(0,0, w->width(), w->height());
} else {
p.drawLine(w->width(), 0, 0, w->height());
}
return false;
}
return false;
}
private:
bool m_tl;
};
#include <QApplication>
int main(int argc, char **argv){
QApplication app(argc, argv);
Dummy d(true);
Dummy u(false);
PushButton pb;
pb.setText("TESTING");
pb.installPostEventFilter(&d);
pb.installPostEventFilter(&u);
pb.show();
return app.exec();
}
BTW. The code is not idiot-proof :) For instance don't try deleting object registered as filters to other objects.
The idea is to have something similar to eventFilter() and friends but different in that way that it is executed after the event has been delivered (contrary to eventFilter that is executed before the event has been delivered). My example usecase is demonstrated with the code below and the screenshot attached.
Also attached you'll find a file containing a macro that emulates "post event filters" on "user" level for single widget classes. To use it just include it in your code, make a widget subclass and add a WW_POSTEVENTFILTER macro just below the Q_OBJECT macro. When implementing postEventFilter() remember that it has to be a public slot (I'm using invokeMethod() to call the method without knowing its class name).
Here is an example:
#include "posteventfilter.h"
class PushButton : public QPushButton {
Q_OBJECT
WW_POSTEVENTFILTER(QPushButton)
public:
PushButton(QWidget *parent=0) : QPushButton(parent){}
};
class Dummy : public QObject {
Q_OBJECT
public:
Dummy(bool tl, QObject *parent=0) : QObject(parent){ m_tl = tl;}
public slots:
virtual bool postEventFilter(QObject *o, QEvent *e){
if(e->type()==QEvent::Paint){
QWidget *w = (QWidget*)o;
QPainter p(w);
QPen pe = p.pen();
pe.setColor(m_tl ? Qt::red : Qt::blue );
pe.setWidth(3);
p.setPen(pe);
p.setRenderHint(QPainter::Antialiasing);
if(m_tl){
p.drawLine(0,0, w->width(), w->height());
} else {
p.drawLine(w->width(), 0, 0, w->height());
}
return false;
}
return false;
}
private:
bool m_tl;
};
#include <QApplication>
int main(int argc, char **argv){
QApplication app(argc, argv);
Dummy d(true);
Dummy u(false);
PushButton pb;
pb.setText("TESTING");
pb.installPostEventFilter(&d);
pb.installPostEventFilter(&u);
pb.show();
return app.exec();
}
BTW. The code is not idiot-proof :) For instance don't try deleting object registered as filters to other objects.