Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Wait on event
Forum Updated to NodeBB v4.3 + New Features

Wait on event

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 3 Posters 2.3k Views 2 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

    Hi,

    I want to create delay of 25 seconds (random value) for syncing purpose.

    But at the same, I dont want UI to get blocked for 25 seconds, it should end the sleep as soon as user operates. For example, mousePress event.

    Is there any implementation present by default in Qt?

    Like, waitForEvent..

    Thanks,
    Kumara

    --Kumar

    raven-worxR kshegunovK 2 Replies Last reply
    0
    • K kumararajas

      Hi,

      I want to create delay of 25 seconds (random value) for syncing purpose.

      But at the same, I dont want UI to get blocked for 25 seconds, it should end the sleep as soon as user operates. For example, mousePress event.

      Is there any implementation present by default in Qt?

      Like, waitForEvent..

      Thanks,
      Kumara

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @kumararajas

      qApp->installEventFilter( fitlerObject );
      

      Then you can inspect every event before it is sent to the receiver object.
      So in there check for mouse button press events for example. Bbut don't return true if you like the event to be delivered to the receiver object, like in every other event-filter method implementation.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • K kumararajas

        Hi,

        I want to create delay of 25 seconds (random value) for syncing purpose.

        But at the same, I dont want UI to get blocked for 25 seconds, it should end the sleep as soon as user operates. For example, mousePress event.

        Is there any implementation present by default in Qt?

        Like, waitForEvent..

        Thanks,
        Kumara

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

        @kumararajas

        class EventWaiter : public QObject
        {
            Q_OBJECT
        
             static const int timeout = 25000; // 25 secs
        
        public:
            EventWaiter()
            {
                qApp->installEventFilter(this); //< This is discouraged, use at your own risk
            }
        
            ~EventWaiter()
            {
                qApp->removeEventFilter(this);
            }
        
            bool eventFilter(QObject * watched, QEvent * event) override
            {
                Q_UNUSED(watched);
                switch(event->type())
                {
                case QEvent::MouseMove:
                case QEvent::MouseButtonPress:
                // ... more events ...
                    emit eventCaptured();
                    // Fallthrough!
                default:
                    return false;
                }
            }
        
            void waitForEvents()
            {
                QObject::connect(this, &EventWaiter::eventCaptured, &loop, &QEventLoop::quit);
                QTimer::singleShot(timeout, &loop, &QEventLoop::quit);
                loop.exec(); //< This is discouraged, use at your own risk
            }
        
        signals:
            void eventCaptured();
        
        private:
            QEventLoop loop;
        };
        

        Use as:

        EventWaiter waiter;
        waiter.waitForEvents();
        

        Read and abide by the Qt Code of Conduct

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

          Thanks for the details! Above answers, makes sense for me!

          Thank you again!

          Kumara

          --Kumar

          raven-worxR 1 Reply Last reply
          0
          • K kumararajas

            Thanks for the details! Above answers, makes sense for me!

            Thank you again!

            Kumara

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @kumararajas
            an alternative (to avoid the event filter) would be to subclass QApplication and override the notify() method. Every event is routed through this method. Thus you do not introduce any extra overhead.
            But anyway the event-filter approach IMO has hardly any noticeable performance impact.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            K 1 Reply Last reply
            0
            • raven-worxR raven-worx

              @kumararajas
              an alternative (to avoid the event filter) would be to subclass QApplication and override the notify() method. Every event is routed through this method. Thus you do not introduce any extra overhead.
              But anyway the event-filter approach IMO has hardly any noticeable performance impact.

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

              @raven-worx This is kinda cool! Thanks for the idea. I am going to try this as well.
              I will keep the results posted. here.

              Thanks!

              --Kumar

              1 Reply Last reply
              0

              • Login

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