How to use eventFilter()?



  • in A widget,i use
    @
    virtual void mousePressEvent(QMouseEvent *);
    virtual void mouseReleaseEvent(QMouseEvent *);
    virtual void mouseDoubleClickEvent(QMouseEvent *);
    @
    but i find i can't distinguish clike and doubleclike,so i want to use eventFilter() to distinguish. but i write
    @
    installEventFilter(this); // this is a widget,
    bool CSSEMainViewPro::eventFilter(QObject *object, QEvent event)
    {
    qDebug()<<"eventFilter";
    if (object == this)
    {
    if(event->type() == QEvent::MouseButtonPress)
    {
    QMouseEvent
    pMouseEvent = static_cast<QMouseEvent >(event);
    mousePressEvent( pMouseEvent );
    return true;
    }
    if(event->type() == QEvent::MouseButtonRelease)
    {
    QMouseEvent
    pMouseEvent = static_cast<QMouseEvent >(event);
    mouseReleaseEvent(pMouseEvent);
    return true;
    }
    if(event->type() == QEvent::MouseButtonDblClick)
    {
    QMouseEvent
    pMouseEvent = static_cast<QMouseEvent *>(event);
    mouseDoubleClickEvent(pMouseEvent);
    return true;
    }
    }
    return QWidget::eventFilter(object,event);
    }
    @
    but eventFilter()can't be use .how to use it?

    edit: fixed your use of @ tags;Andre



  • Is CSSEMainViewPro a subclass of QWidget?

    BTW,

    1. What you do is exactly the default behavior of mousePressEvent(...),...
    2. There is no need to install a eventfilter to itself, all the event can be dealt with in event() .


  • but i want to distinguish clike and doubleclike,how to do ?QTimer can't come ture;and i must use
    @
    virtual void mousePressEvent(QMouseEvent *);
    virtual void mouseReleaseEvent(QMouseEvent *);
    virtual void mouseDoubleClickEvent(QMouseEvent *)
    @
    yes,CSSEMainViewPro is a subclass of QWidget?



  • In fact, there is no way to distinguish a mouse press-release is a single-click or part of a double-click.

    press ... release(single-click) ... press ... release(double-click)

    You don't know whether the first press...release is a real single-click or not, unless you wait a short time to make sure that there will no another press...release.



  • There is no way to distinguish, as 1+1=2 rightly says. Normally, a single click would be a select/make current action, while the double click would perform a default action on that (now current) item. So, the click and double click would be a logical sequence.

    If you can't make your application work that way, the only thing you can do is to not directly execute the action attached to the click. Instead, you'd wait a little to see if the click turns into a double click or not. Anyway, you're not the first to ask this question. I posted my solution in "a previous topic":/forums/viewthread/26039 on this issue. Note that it does use eventFilter, but not in the way you tried to use it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.