Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Do not process QTimer slots while waiting for network response
Qt 6.12 LTS is out! See what's new in the release blog

Do not process QTimer slots while waiting for network response

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 739 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.
  • T Offline
    T Offline
    Tarae
    wrote on last edited by
    #1

    Hello,

    I have my object, subclass of QObject in a seperate thread.
    This object uses QNetworkAccessManager to get data from remote server.

    This is how my code looks like:

      auto reply = networkAccessManager.get(myRequest);
      QEventLoop loop;
      connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
      loop.exec(QEventLoop::ExcludeSocketNotifiers);
      doSomethngWithData(reply);
    

    But to make things more complicated, my class also contains a QTimer. And I don't want to process timeout events while waiting for data. I don't know how to do that.

    I hoped QEventLoop::ExcludeSocketNotifiers would postpone timer's slot execution until after the data is loaded and processed, but it doesn't work that way.

    The only solution I'm thinking about is to have a boolean isLoadingData variable and a queue for timer events, store them there, and process them afterward.

    Is there a better way?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not stop the timer when starting the request and start it again when it's done processing ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tarae
        wrote on last edited by
        #3

        Hi, thank you for your idea. That would probably work. But in my case, it is complicated. There are actually like 5 timers, some of them active and some not. Storing the active/inactive state and remaining time for each, then restoring them... I would probably create a timer subclass PausableTimer, but it's still complicated. If there were an easier way, I would prefer it.

        Pl45m4P 1 Reply Last reply
        0
        • T Tarae

          Hi, thank you for your idea. That would probably work. But in my case, it is complicated. There are actually like 5 timers, some of them active and some not. Storing the active/inactive state and remaining time for each, then restoring them... I would probably create a timer subclass PausableTimer, but it's still complicated. If there were an easier way, I would prefer it.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @Tarae

          Hi,

          what about this?

            auto reply = networkAccessManager.get(myRequest);
            const QSignalBlocker blocker(yourTimer);
            connect(&networkAccessManager, &QNetworkAccessManager::finished, this, [=](QNetworkReply *reply){
                // blocker.unblock(); // maybe not even needed
                doSomethngWithData(reply);
            });
          

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          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
          • Unsolved