Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to know if the events are generated by application or Qt

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.3k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kumararajas
    wrote on last edited by
    #1

    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!

    --Kumar

    C kshegunovK 2 Replies Last reply
    0
    • K kumararajas

      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!

      C Offline
      C Offline
      c64zottel
      wrote on last edited by
      #2

      @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?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kumararajas
        wrote on last edited by
        #3

        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.

        --Kumar

        C 1 Reply Last reply
        0
        • K kumararajas

          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.

          C Offline
          C Offline
          c64zottel
          wrote on last edited by c64zottel
          #4

          @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;

          K 1 Reply Last reply
          2
          • C c64zottel

            @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;

            K Offline
            K Offline
            kumararajas
            wrote on last edited by
            #5

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

            --Kumar

            1 Reply Last reply
            1
            • K kumararajas

              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!

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @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.

              Read and abide by the Qt Code of Conduct

              K 1 Reply Last reply
              2
              • kshegunovK kshegunov

                @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.

                K Offline
                K Offline
                kumararajas
                wrote on last edited by
                #7

                @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!

                --Kumar

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved