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

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Request for comment: Post event filter

    Quote Originally Posted by wysota View Post
    For instance don't try deleting object registered as filters to other objects.
    And don't try to use that macro for a class that doesn't inherit QPushButton.

    Anyway it's nice and it looks similar to Java event handlers, but wouldn't it encourage people to create inefficient solutions? Like having decorators that paint on widgets (each with its own painter) instead of having proper custom widget. Situation is a bit similar to QxxxWidget classes --- first you use QxxxWidget because it's simplier, but then when code gets more complex you don't have time to switch to proper solution.

  3. #3
    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 Re: Request for comment: Post event filter

    Quote Originally Posted by jacek View Post
    And don't try to use that macro for a class that doesn't inherit QPushButton.
    Baah... you're right, I missed the fact that I'm calling the base class implementation... Probably this can only be done in Qt kernel by implementing this on QObject level...

    Anyway it's nice and it looks similar to Java event handlers, but wouldn't it encourage people to create inefficient solutions?
    Hmm... Not more than regular event filters, right?

    Like having decorators that paint on widgets (each with its own painter) instead of having proper custom widget.
    Actually in Qt the painter is shared across calls, so that's not an issue. And the code I provided is only a use case - provide an overlay for a widget (I don't like using QPixmap::grabWidget). I can imagine it being used for drawing rubber bands too. There are probably other use-cases not related to drawing, that's why I posted this thread in the first place...

    Situation is a bit similar to QxxxWidget classes --- first you use QxxxWidget because it's simplier, but then when code gets more complex you don't have time to switch to proper solution.
    I think it depends on what you are doing. I don't think event pre-filters are misused, so the probability of event post-filters (or rather post-hooks as the event is not being discarded) being misused is more or less similar.

    Ok, I'm uploading a modified code so that it works with any object class, not only QPushButton Now you have to pass the superclass name derived from QObject as a parameter to the macro.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Request for comment: Post event filter

    Quote Originally Posted by wysota View Post
    I don't think event pre-filters are misused, so the probability of event post-filters (or rather post-hooks as the event is not being discarded) being misused is more or less similar.
    I'm not comparing pre- and post-filters. I just wonder whether people won't choose such post-filter instead of reimplementing an event handler.

    Your post-filter would prove themselves if there were some generic handlers/decorators that could be attached to different objects.

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.