How to know if the events are generated by application or Qt



  • Hello all,

    bool MyClass:event( QEvent * event )
    {
        qDebug() << "Event type: " << event->type();
        return QWidget::event(event);
    }
    

    With the above code snippet, I can understand the type of event. Is there a way that I can know if the event is generated by Qt or by the application?

    Thanks!



  • @kumararajas What are you trying to do in general? Are you going to hook into the main even loop to filter out some events you send from somewhere else?



  • Nope, just want to understand how to differentiate between the events that are generated by Qt vs by the application.

    Reason is not clear, but I can make decisions accordingly, if needed.



  • @kumararajas Ok, my advice is not to do this and use an independent road to a) save resources and b) have clearer design.

    That being said, since QEvent has a virtual dtor, it is save to inherit from it. That way you simple try to dynamic_cast it to your custom type. If the casting is not successful, then it is simply not you custom type and you simply return.

    Untested:

    class MyBigEvent : public QEvent
    {
    int importantInt;
    }

    Your object dealing with it:

    bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
    MyBigEvent * p dynamic_cast< MyBigEvent * >( event );

    if( not p )
    return QObject::eventFilter(obj, event); // give your parent a chance to deal with it

    // do cool stuff with your event here

    return true;



  • @c64zottel Okay, cool, Thanks for the thoughts! :)


  • Qt Champions 2016

    @kumararajas said in How to know if the events are generated by application or Qt:

    Is there a way that I can know if the event is generated by Qt or by the application?

    Not really, no. You can "know" if the event originated from the window manager/OS (i.e. outside of Qt or your app) by querying if the event's spontaneous - QEvent::spontaneous.



  • @kshegunov This is a cool stuff! I like it. This answers me and solves my problem technically! Thank you! I can try out and keep you posted with the results. The outcome should be good as per the feel!


Log in to reply
 

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