title: Qt: How to delay a program while QTimer is active?
body: <p>Please refer to the following link as I tried to simplify the problem I was having and now have run into a problem that I cannot solve.</p>
  
  <p>Link: <a href="https://stackoverflow.com/questions/4695044/qt-how-to-use-qtimer-to-print-a-message-to-a-qtextbrowser-every-10-seconds">Qt: How to use QTimer to print a message to a QTextBrowser every 10 seconds?</a></p>
  
  <p>In the posting for the link above I simplified the task I am trying to do by simply saying that I wanted to push a button and have it display something in a <code>QTextBrowser</code> every 10 seconds. I was having trouble getting <code>QTimer</code> working at the time so I thought that if I could get <code>QTimer</code> to work then I could finish my task.</p>
  
  <p>What I am really trying to do is read lines from a file and after every 2500 lines I would like to print a message and then wait 10 seconds.</p>
  
  <p>Pseudocode:</p>
  
  <pre><code>while(not at the end of the file)
  {
     read in 2500 lines 
  
     print a message
  
     wait 10 seconds
  }
  </code></pre>
  
  <p><code>QTimer</code> is nice but it does opposite of what I want it to do. Instead of transmitting a message and waiting 10 seconds, first they wait 10 seconds, timeout and then send the message.</p>
  
  <p>So to get it to work the way I want I first called the <code>printMessage()</code> SLOT and then I made a SLOT called <code>stopTimer()</code> that simply stops the timer. So after the 10 seconds has passed it will simply call stopTimer() and then continue processing the input file.</p>
  
  <p>Onto the REAL problem:</p>
  
  <p>Qt does not wait for a <code>QTimer</code> to finish before it moves on through the code. I want the code to wait the full 10 seconds before it reads the next 2500 lines of code.  I found that <code>QTimer</code> has an <code>isActive()</code> function that returns a bool value. </p>
  
  <p>So in the place where I wanted the 10 second delay to finish I put the following:</p>
  
  <pre><code>while(timer-&gt;isActive());
  </code></pre>
  
  <p>I figured the program would stay in this loop for 10 seconds and then exit after the <code>QTimer</code> timed out because the condition would then be false. The problem is that it doesn’t exit the loop because the status of the timer never changes regardless of how long it waits in this loop! I checked with the debugger and <code>isActive( )</code> remains true regardless of elapsed time.</p>
  
  <p>I then omitted the <code>while(timer-&gt;isActive())</code> loop and observed the timer in the debugger. It seems that the timer does not actually start timing until it exits the while(not at the end of the file). So I believe that since the <code>while(timer-&gt;isActive())</code> loop is nested inside this, it is causing it to never timeout. I could be wrong but this is what seems like is happening. Also, what is annoying is that the <code>QTimer</code> object has no field that shows the elapsed time of the timer when it is active. Therefore, I cannot check the elapsed time at all to further debug.</p>
  
  <p>Someone please test this out or let me know a workaround for this!</p>
  
  <p>For something that sounds so easy, this has been the biggest pain I have had in recent time, but I generally don’t use Qt so it could be my lack of experience.</p>
  
  <p>Here is an excerpt from code I have which currently freezes as stated above:</p>
  
  <pre><code>void Form::startBtn_pushed()
  {
      QTimer *timer = new QTimer(this);
      QFile file(“file.txt”);
      QTextStream stream(&amp;file);
      QString line;
      int lineCount = 0;
  
      connect(timer, SIGNAL(timeout()), this, SLOT(stopTimer()));
  
      while(!(stream.atEnd())
      {
         line = stream.readLine();
         lineCount++;
  
         if(lineCount == 2500)
         { 
            printMessage();
  
            timer-&gt;start(10000);
  
            while(timer-&gt;isActive()); //Wait for timer to timeout before continuing
         }
      }
  }
  </code></pre>
  
  <p>Sorry for the long post and if I might have made any grammatical errors with my code here. I do not have internet access on my dev machine so I had to retype this here.</p>