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. QJsondocument slowness when hooked up to a QSlider
Qt 6.11 is out! See what's new in the release blog

QJsondocument slowness when hooked up to a QSlider

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 7 Posters 5.7k Views 3 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.
  • jsulmJ jsulm

    @Kris-Revi OK, watched once more - yes, there is one change at the end of the video. It should work with QNetwork & QJsonDocument.
    Can you post your current code?

    K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #10

    @jsulm

    class software(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            QtWidgets.QWidget.__init__(self, parent)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.Brightnessslider.valueChanged.connect(self.adjustBrightness)
    
            self.nam = QtNetwork.QNetworkAccessManager()
    
        def doPost(self, toUrl, tojson):
            try:
                print(tojson)
                document = QJsonDocument(tojson)
    
                request = QtNetwork.QNetworkRequest(QtCore.QUrl(toUrl))
                request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json; charset=UTF-8")
    
                self.nam.post(request, document.toJson())
            except Exception as e:
                print(e)
    
        def adjustBrightness(self):
            self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': self.ui.Brightnessslider.value()})
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #11

      One thing comes to mind: QNAM handles up to 6 requests in parallel and then queues the other ones to be execute as soon as possible. requests as well as httplib2 do not have that kind of consideration as they are not asynchronous therefore I think that what you might be observing might be related to architectural differences.

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

      K 1 Reply Last reply
      2
      • SGaistS SGaist

        One thing comes to mind: QNAM handles up to 6 requests in parallel and then queues the other ones to be execute as soon as possible. requests as well as httplib2 do not have that kind of consideration as they are not asynchronous therefore I think that what you might be observing might be related to architectural differences.

        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #12

        @SGaist and sadly there is nothing i can do there? :/ nothing i can turn off or adjust?

        mrjjM 1 Reply Last reply
        0
        • K Kris Revi

          @SGaist and sadly there is nothing i can do there? :/ nothing i can turn off or adjust?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #13

          @Kris-Revi
          Hi
          As far as i know, you cannot alter the limit of 6.

          1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #14

            I'm pretty sure that's not a problem of QNAM but somewhere else. Maybe the device does not answer fast enough or so so that QNAM is waiting for the response very long.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            K 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              I'm pretty sure that's not a problem of QNAM but somewhere else. Maybe the device does not answer fast enough or so so that QNAM is waiting for the response very long.

              K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #15

              @Christian-Ehrlicher sadly it is QNAM! when using "requests", "httplib2" or "urllib3" it is INSTANT response! no queue or waiting!

              mrjjM 1 Reply Last reply
              0
              • K Kris Revi

                @Christian-Ehrlicher sadly it is QNAM! when using "requests", "httplib2" or "urllib3" it is INSTANT response! no queue or waiting!

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #16

                @Kris-Revi
                And the other end uses the same parsing and reaction code regards less of you using
                httplib2 or NAM ?
                So the only difference is the sending?

                K 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Kris-Revi
                  And the other end uses the same parsing and reaction code regards less of you using
                  httplib2 or NAM ?
                  So the only difference is the sending?

                  K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by
                  #17

                  @mrjj exactly!

                  mrjjM 1 Reply Last reply
                  0
                  • K Kris Revi

                    @mrjj exactly!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #18

                    @Kris-Revi
                    Well its very likely SGaist is right and what you see is related to architectural differences.
                    NAM is async so pr request it will have a slight delay before actually sending compared to
                    a lib which is blocking and sends at once.
                    That said its normally very fast and even the slider will generate heaps of
                    signals, it should fast catch up if parsing is fast enough.

                    If you limit your sending to 6 outstanding, does it then react almost as fast ?

                    i mean something like.
                    self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 10})
                    self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 20})
                    self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 30})
                    self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 40})
                    self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 50})
                    self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 60})

                    K 1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by Christian Ehrlicher
                      #19

                      You must be something doing very wrong:

                      static int s_requestFinishedCount = 0;
                      static const int s_maxRequests = 100;
                      
                      int main(int argc, char *argv[])
                      {
                          QCoreApplication app(argc, argv);
                          QElapsedTimer timer;
                          timer.start();
                          QNetworkAccessManager nam;
                          QNetworkRequest req(QUrl::fromUserInput("http://192.168.178.200/"));
                          for (int i = 0; i < s_maxRequests; ++i)
                          {
                            QNetworkReply *reply = nam.get(req);
                            QObject::connect(reply, &QNetworkReply::finished,
                                             reply, []()
                            {
                              qDebug() << "s_requestFinishedCount:" << s_requestFinishedCount;
                              if (++s_requestFinishedCount == s_maxRequests)
                                QCoreApplication::quit();
                            });
                            QObject::connect(reply, &QNetworkReply::finished,
                                             reply, &QNetworkReply::deleteLater);
                          }
                          int ret =app.exec();
                          qDebug() << "Elapsed:" << timer.elapsed() << ", ret: " << ret;
                      
                          return ret;
                      }
                      

                      --> 580ms

                      shell:

                      date
                      for start in {1..100};
                      do
                        wget -nv http://192.168.178.200 2>&1 > /dev/null
                      done
                      date
                      

                      --> 11 seconds

                      The ip is a raspi 1 delivering a very small website

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @Kris-Revi
                        Well its very likely SGaist is right and what you see is related to architectural differences.
                        NAM is async so pr request it will have a slight delay before actually sending compared to
                        a lib which is blocking and sends at once.
                        That said its normally very fast and even the slider will generate heaps of
                        signals, it should fast catch up if parsing is fast enough.

                        If you limit your sending to 6 outstanding, does it then react almost as fast ?

                        i mean something like.
                        self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 10})
                        self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 20})
                        self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 30})
                        self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 40})
                        self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 50})
                        self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': 60})

                        K Offline
                        K Offline
                        Kris Revi
                        wrote on last edited by
                        #20

                        @mrjj sadly it looks like if i can't controll / adjust / remove the queue system from QNAM im stuck using either "requests", "httplib2" or "urllib3" as they are instant :/ i can't have that queue system it takes ages befor the brightness levels of the LED's are where the slider is set to :/

                        kshegunovK 1 Reply Last reply
                        0
                        • K Kris Revi

                          @mrjj sadly it looks like if i can't controll / adjust / remove the queue system from QNAM im stuck using either "requests", "httplib2" or "urllib3" as they are instant :/ i can't have that queue system it takes ages befor the brightness levels of the LED's are where the slider is set to :/

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

                          @Kris-Revi said in QJsondocument slowness when hooked up to a QSlider:

                          @mrjj sadly it looks like if i can't controll / adjust / remove the queue system from QNAM im stuck using either "requests", "httplib2" or "urllib3" as they are instant :/ i can't have that queue system it takes ages befor the brightness levels of the LED's are where the slider is set to :/

                          I'm pretty sure with QNAM you're flooding the device sending near 10 times the payload (i.e. you're DoS-ing your device). What you can and should do instead is to disentangle the slider from the communication and buffer the slider's events instead of flushing them directly through the network. That is you introduce a timer that ticks every 200ms or so (which is below the typical reaction time of a human anyway), and send a message to the device if the slider's value has changed in the meantime. In Qt this'd look something like this:

                          QObject::connect(slider, &QAbstractSlider::valueChanged, controller, &ControllerClass::setValue);
                          QObject::connect(timer, &QTimer::timeout, controller, &ControllerClass::sendNetworkData);
                          

                          where all the slots and signals do the obvious - the ControllerClass::setValue just saves the new value and if it's different from the previous raises an internal dirty state. ControllerClass::sendNetworkData checks if the state is dirty, and if so, sends the new data and resets the flag.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          3

                          • Login

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