Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. GUI Freezes When Loop is Started
QtWS25 Last Chance

GUI Freezes When Loop is Started

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqml dynamicloopfunctionthread
7 Posts 2 Posters 1.7k Views
  • 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.
  • C Offline
    C Offline
    closx
    wrote on last edited by
    #1

    Hello guys,

    I am trying to code a LED light transition application.
    I have 2 functions to proceed.
    One is for the delay, one is for the transition.

        function delayme(duration) {
            var timeStart = new Date().getTime();
            while (new Date().getTime() - timeStart < duration) {
            }
        }
    
        function transitme(r,g,b){
            for(var i=0;i<255;i++){
                myColor=Qt.rgba(r/255, g+i/255, b/255,1);
                sendColor(myColor);
                delayme(100);
                console.log("loop no:" +i);
            }
        }
    

    So, if I call the function as transitme(255,0,0) the lights do change
    from red to yellowish, but when de loop starts, GUI freezes so you cannot
    see the transitting.

    note:
    myColor is the colour variable that you see in GUI.
    sendColor() is the function to send the colours to the LEDs.
    me is someone that does not understand from threads

    bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
    tag me (like @closx) if you are answering to me, so I can notice :D

    jsulmJ 1 Reply Last reply
    0
    • C closx

      Hello guys,

      I am trying to code a LED light transition application.
      I have 2 functions to proceed.
      One is for the delay, one is for the transition.

          function delayme(duration) {
              var timeStart = new Date().getTime();
              while (new Date().getTime() - timeStart < duration) {
              }
          }
      
          function transitme(r,g,b){
              for(var i=0;i<255;i++){
                  myColor=Qt.rgba(r/255, g+i/255, b/255,1);
                  sendColor(myColor);
                  delayme(100);
                  console.log("loop no:" +i);
              }
          }
      

      So, if I call the function as transitme(255,0,0) the lights do change
      from red to yellowish, but when de loop starts, GUI freezes so you cannot
      see the transitting.

      note:
      myColor is the colour variable that you see in GUI.
      sendColor() is the function to send the colours to the LEDs.
      me is someone that does not understand from threads

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

      @closx Loops in GUI thread block the GUI.
      Instead of using such a loop use a timer with regular timeout and on each timeout run the code which you have now in your loop.

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

      C 1 Reply Last reply
      3
      • jsulmJ jsulm

        @closx Loops in GUI thread block the GUI.
        Instead of using such a loop use a timer with regular timeout and on each timeout run the code which you have now in your loop.

        C Offline
        C Offline
        closx
        wrote on last edited by
        #3

        @jsulm Thanks for the light-speed answer!
        I have already tried using a timer. The fact is, I need an index number that I can use.
        So I can change colours in time. I think, I do not have an index value in timers.

        note:
        Tried to write a c++ function with a loop and integrate it with qml, too. Result is just the same.

        bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
        tag me (like @closx) if you are answering to me, so I can notice :D

        jsulmJ 1 Reply Last reply
        0
        • C closx

          @jsulm Thanks for the light-speed answer!
          I have already tried using a timer. The fact is, I need an index number that I can use.
          So I can change colours in time. I think, I do not have an index value in timers.

          note:
          Tried to write a c++ function with a loop and integrate it with qml, too. Result is just the same.

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

          @closx said in GUI Freezes When Loop is Started:

          The fact is, I need an index number that I can use

          Then use one. Just define a variable, initialize it with 0 before staring the timer and each time the timeout comes increase it.

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

          C 1 Reply Last reply
          3
          • jsulmJ jsulm

            @closx said in GUI Freezes When Loop is Started:

            The fact is, I need an index number that I can use

            Then use one. Just define a variable, initialize it with 0 before staring the timer and each time the timeout comes increase it.

            C Offline
            C Offline
            closx
            wrote on last edited by
            #5

            @jsulm Alright,
            I have written a timer like this,

                Timer{
                    id:transitme
                    interval: 1000; //made it 1 second to test it
                    running: false;
                    repeat: true
                    onTriggered: function(){
                        var index=0;
                        myColor=Qt.rgba(255/255, index/255, 0/255,1);
                        sendColor(myColor);
                        console.log("Index is: " + index);
                        index++;
                    }
                }
            

            and I am calling it with

            transitme.running=true;
            transitme.running.false;
            

            but the index is always zero as I guessed. Where else can I define the
            index, so it will not be re-created in every single loop?

            bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
            tag me (like @closx) if you are answering to me, so I can notice :D

            jsulmJ 1 Reply Last reply
            0
            • C closx

              @jsulm Alright,
              I have written a timer like this,

                  Timer{
                      id:transitme
                      interval: 1000; //made it 1 second to test it
                      running: false;
                      repeat: true
                      onTriggered: function(){
                          var index=0;
                          myColor=Qt.rgba(255/255, index/255, 0/255,1);
                          sendColor(myColor);
                          console.log("Index is: " + index);
                          index++;
                      }
                  }
              

              and I am calling it with

              transitme.running=true;
              transitme.running.false;
              

              but the index is always zero as I guessed. Where else can I define the
              index, so it will not be re-created in every single loop?

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

              @closx said in GUI Freezes When Loop is Started:

              Where else can I define the
              index, so it will not be re-created in every single loop?

              Like

              Timer{
                  property var index: 0
              

              https://doc.qt.io/qt-5/qml-var.html

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

              C 1 Reply Last reply
              4
              • jsulmJ jsulm

                @closx said in GUI Freezes When Loop is Started:

                Where else can I define the
                index, so it will not be re-created in every single loop?

                Like

                Timer{
                    property var index: 0
                

                https://doc.qt.io/qt-5/qml-var.html

                C Offline
                C Offline
                closx
                wrote on last edited by
                #7

                @jsulm U are da king!

                I am dealing with QML for months, and just learning the property variables! That kinda saved my life mate!
                Thank you very much!

                bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
                tag me (like @closx) if you are answering to me, so I can notice :D

                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