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. Delay Loop until a QPushButton pressed
Forum Update on Monday, May 27th 2025

Delay Loop until a QPushButton pressed

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.0k 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    I want to iterate through a list of files and have the program wait until a user presses the skip or save push button before going to the next file. I created a boolean variable called wait which would be set to true and changed to false when a push button is pressed. The following is a code extract showing the logic:
    I want to as much as possible i don't used multi thread in qt.

    Qt code :

    for (start=0 ; start<numberOfFiles ; start++)   // main Processing Loop
    {      ...
            wait = true;
            while (wait)
                     ;         // Good For Nothing
            ...
    }
     
    // Assume  QPushButton clicked SIGNAL is properly linked to savedPressed() SLOT.
     
    void BYDATE::savePressed ()    // SLOT
    	wait=false;
    }
    

    The above code causes the program to hang. Is it possible to change the wait variable when the program is in the while loop. I'm hoping to avoid multi-threading, if possible.

    Any suggestions?

    Thanks very much

    aha_1980A kshegunovK 2 Replies Last reply
    0
    • stackprogramerS stackprogramer

      I want to iterate through a list of files and have the program wait until a user presses the skip or save push button before going to the next file. I created a boolean variable called wait which would be set to true and changed to false when a push button is pressed. The following is a code extract showing the logic:
      I want to as much as possible i don't used multi thread in qt.

      Qt code :

      for (start=0 ; start<numberOfFiles ; start++)   // main Processing Loop
      {      ...
              wait = true;
              while (wait)
                       ;         // Good For Nothing
              ...
      }
       
      // Assume  QPushButton clicked SIGNAL is properly linked to savedPressed() SLOT.
       
      void BYDATE::savePressed ()    // SLOT
      	wait=false;
      }
      

      The above code causes the program to hang. Is it possible to change the wait variable when the program is in the while loop. I'm hoping to avoid multi-threading, if possible.

      Any suggestions?

      Thanks very much

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @stackprogramer,

      as stupid as it sounds, you cannot use loops for that problem.

      You need to factor out the content of your loop to a function, and call that function, with one parameter referencing the file and the second parameter, specifying the save or quit action.

      Then update the UI for the next file and quit your function, therefore let the Qt event loop do the waiting for user input for you.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      6
      • stackprogramerS stackprogramer

        I want to iterate through a list of files and have the program wait until a user presses the skip or save push button before going to the next file. I created a boolean variable called wait which would be set to true and changed to false when a push button is pressed. The following is a code extract showing the logic:
        I want to as much as possible i don't used multi thread in qt.

        Qt code :

        for (start=0 ; start<numberOfFiles ; start++)   // main Processing Loop
        {      ...
                wait = true;
                while (wait)
                         ;         // Good For Nothing
                ...
        }
         
        // Assume  QPushButton clicked SIGNAL is properly linked to savedPressed() SLOT.
         
        void BYDATE::savePressed ()    // SLOT
        	wait=false;
        }
        

        The above code causes the program to hang. Is it possible to change the wait variable when the program is in the while loop. I'm hoping to avoid multi-threading, if possible.

        Any suggestions?

        Thanks very much

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

        @stackprogramer said in Delay Loop until a QPushButton pressed:

        Any suggestions?

        Put them strings in a stack/vector. When the button is pressed do whatever you want to do and pop from the stack (or the tail of the vector). If your stack's empty there are no more files to process. As @aha_1980 said, looping around is just not the way to work in event-driven environment.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        4
        • stackprogramerS Offline
          stackprogramerS Offline
          stackprogramer
          wrote on last edited by stackprogramer
          #4

          @stackprogramer said in Delay Loop until a QPushButton pressed:

          ait = true;
          while (wait)
          ;

          @aha_1980

          When i used a function for this situation (I defined a flag, function should check this flag and continue recording file or stop it.)

          This function should record a file. When user clicks stop button, flag should change to false. and i expect file recording function is finished.....
          But with function way other gui signals not work to change flag or stop is recording.
          I emphasize i don't want using multi-thread in qt.

          Christian EhrlicherC aha_1980A 2 Replies Last reply
          -1
          • stackprogramerS stackprogramer

            @stackprogramer said in Delay Loop until a QPushButton pressed:

            ait = true;
            while (wait)
            ;

            @aha_1980

            When i used a function for this situation (I defined a flag, function should check this flag and continue recording file or stop it.)

            This function should record a file. When user clicks stop button, flag should change to false. and i expect file recording function is finished.....
            But with function way other gui signals not work to change flag or stop is recording.
            I emphasize i don't want using multi-thread in qt.

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @stackprogramer Can you please write in complete sentences?

            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
            • stackprogramerS stackprogramer

              @stackprogramer said in Delay Loop until a QPushButton pressed:

              ait = true;
              while (wait)
              ;

              @aha_1980

              When i used a function for this situation (I defined a flag, function should check this flag and continue recording file or stop it.)

              This function should record a file. When user clicks stop button, flag should change to false. and i expect file recording function is finished.....
              But with function way other gui signals not work to change flag or stop is recording.
              I emphasize i don't want using multi-thread in qt.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @stackprogramer

              you can use a QTimer to continously write to a file in chunks without blocking. No need for multithreading.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              2

              • Login

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