View Poll Results: What do you think of the idea of post-event filters?

Voters
8. You may not vote on this poll
  • I find post-event filters useful (please post a use-case if you can think of one)

    2 25.00%
  • I have no opinion on the subject or I don't understand what post-event filters are

    6 75.00%
  • I don't think the idea is useful (why?)

    0 0%
Results 1 to 4 of 4

Thread: Request for comment: Post event filter

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Request for comment: Post event filter

    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

    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:
    Qt Code:
    1. #include "posteventfilter.h"
    2.  
    3. class PushButton : public QPushButton {
    4. Q_OBJECT
    5. WW_POSTEVENTFILTER(QPushButton)
    6. public:
    7. PushButton(QWidget *parent=0) : QPushButton(parent){}
    8. };
    9.  
    10. class Dummy : public QObject {
    11. Q_OBJECT
    12. public:
    13. Dummy(bool tl, QObject *parent=0) : QObject(parent){ m_tl = tl;}
    14. public slots:
    15. virtual bool postEventFilter(QObject *o, QEvent *e){
    16. if(e->type()==QEvent::Paint){
    17. QWidget *w = (QWidget*)o;
    18. QPainter p(w);
    19. QPen pe = p.pen();
    20. pe.setColor(m_tl ? Qt::red : Qt::blue );
    21. pe.setWidth(3);
    22. p.setPen(pe);
    23. p.setRenderHint(QPainter::Antialiasing);
    24. if(m_tl){
    25. p.drawLine(0,0, w->width(), w->height());
    26. } else {
    27. p.drawLine(w->width(), 0, 0, w->height());
    28. }
    29. return false;
    30. }
    31. return false;
    32. }
    33. private:
    34. bool m_tl;
    35. };
    36.  
    37. #include <QApplication>
    38.  
    39. int main(int argc, char **argv){
    40. QApplication app(argc, argv);
    41. Dummy d(true);
    42. Dummy u(false);
    43. PushButton pb;
    44. pb.setText("TESTING");
    45. pb.installPostEventFilter(&d);
    46. pb.installPostEventFilter(&u);
    47. pb.show();
    48. return app.exec();
    49. }
    To copy to clipboard, switch view to plain text mode 

    BTW. The code is not idiot-proof For instance don't try deleting object registered as filters to other objects.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by wysota; 6th March 2008 at 00:01. Reason: Updated attachment and code

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.