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
Forum Updated to NodeBB v4.3 + New Features

QJsondocument slowness when hooked up to a QSlider

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 7 Posters 3.0k 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by AndyS
    #2

    [removed unnecessary phrasing that was not directly related to the problem at hand - @AndyS]

    instead of passing each change value during the drag, only do the post on the final slider value after done sliding.

    K 1 Reply Last reply
    5
    • K Kris Revi

      So i just got the QJsondocument thing working and tested it on a QSlider but damn it's slow, can anyone tell me why?

      what this program is controlling is an arduino that has LED's on it and when i slide the QSlider from 0 to 255 i see the LED's SLOWLY ramping up the brightness (it's not happening like "on the fly") if you understand, what causes this? is there anything i can set to make it go instantly?

      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.nam2 = 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.nam2.post(request, document.toJson())
              except Exception as e:
                  print(e)
      
          def adjustBrightness(self):
              self.doPost(f"{self.boardURL}/ledBrightness", {'brightness': self.ui.Brightnessslider.value()})
      
      
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #3

      Hi,

      Did you check the network speed ?
      The device reactivity ? It might not really appreciate getting so many connections per second. Depending on what it is, it might even be seen as a DOS attack.

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

      cfg = self.nam2.activeConfiguration()
      cfg.setConnectTimeout(1000)

      Why are you doing this for all calls ? You were already explained that it should be done once as it will apply to all connections made using that configuration.

      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
      2
      • Kent-DorfmanK Kent-Dorfman

        [removed unnecessary phrasing that was not directly related to the problem at hand - @AndyS]

        instead of passing each change value during the drag, only do the post on the final slider value after done sliding.

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

        @Kent-Dorfman well yea, when "requests" and "httplib2" can do it i thought why can't Qt do it!

        by using this:

        url_json = self.boardURL + "/selectPattern"
        headers = {"Content-Type": "application/json; charset=UTF-8"}
        data = {"selectedPattern": self.selectedPattern}
        response, content = http.request(url_json, "POST", headers=headers, body=json.dumps(data))
        

        i get instant respons from the arduino! the only thing is i get a slight "lagg" on the slider itself! So that's why i thought i could give QNetwork and QJsonDocument a try!

        @SGaist as mentioned above using "requests" and "httplib2" i get instant response no delay or "slowness" (with the little downside of the slider lagging sometimes)
        yea about the connectionTimeout that is not supposed to be there stupid me just tryed to test it! (removed now)

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

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

          i get instant response no delay or "slowness"

          How do you measure? Moving a slider generates a lot of events...

          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
          1
          • Christian EhrlicherC Christian Ehrlicher

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

            i get instant response no delay or "slowness"

            How do you measure? Moving a slider generates a lot of events...

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

            @Christian-Ehrlicher i measure by looking at the LED's how fast they go bright when i slide the slider using the 2 methods

            this is using QNetwork & QJsonDocument (Notice how slow and long it takes for the LED's to go full brightness and then off again)
            https://vimeo.com/400919786

            and here i am using requests and httplib2 (notice that when moving the slider it goes instantly! no delay or "slowness")
            https://vimeo.com/400920348

            jsulmJ 1 Reply Last reply
            0
            • K Kris Revi

              @Christian-Ehrlicher i measure by looking at the LED's how fast they go bright when i slide the slider using the 2 methods

              this is using QNetwork & QJsonDocument (Notice how slow and long it takes for the LED's to go full brightness and then off again)
              https://vimeo.com/400919786

              and here i am using requests and httplib2 (notice that when moving the slider it goes instantly! no delay or "slowness")
              https://vimeo.com/400920348

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Kris-Revi When using QNetwork & QJsonDocument I don not see ANY changes on your LEDs, so my guess is that your code with QNetwork & QJsonDocument is not working properly.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply
              1
              • jsulmJ jsulm

                @Kris-Revi When using QNetwork & QJsonDocument I don not see ANY changes on your LEDs, so my guess is that your code with QNetwork & QJsonDocument is not working properly.

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

                @jsulm whaat? maybe it was hard to see on the video but you could see it, it's there!

                QNetwork & QJsonDocument
                Moves the slider from 0 to 255
                Send 1 -> Wait -> No Error
                Send 2 -> Wait -> No Error
                Send 3 -> Wait -> No Error
                Send 4 -> Wait -> No Error
                Send 5 -> Wait -> No Error
                and so on
                

                it sends the data 1 at a time and then waiting for something and then it sends another one and waits and so on...

                Using requests and httplib2
                Moves the slider from 0 to 255
                Send 1
                Send 2
                Send 3
                Send 4
                Send 5
                

                here it just smacks it in with no waiting time

                jsulmJ 1 Reply Last reply
                0
                • K Kris Revi

                  @jsulm whaat? maybe it was hard to see on the video but you could see it, it's there!

                  QNetwork & QJsonDocument
                  Moves the slider from 0 to 255
                  Send 1 -> Wait -> No Error
                  Send 2 -> Wait -> No Error
                  Send 3 -> Wait -> No Error
                  Send 4 -> Wait -> No Error
                  Send 5 -> Wait -> No Error
                  and so on
                  

                  it sends the data 1 at a time and then waiting for something and then it sends another one and waits and so on...

                  Using requests and httplib2
                  Moves the slider from 0 to 255
                  Send 1
                  Send 2
                  Send 3
                  Send 4
                  Send 5
                  

                  here it just smacks it in with no waiting time

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply
                  0
                  • 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 Online
                            Christian EhrlicherC Online
                            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 Online
                                      Christian EhrlicherC Online
                                      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