Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3840990
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:35:39+00:00 2026-05-19T15:35:39+00:00

Please refer to the following link as I tried to simplify the problem I

  • 0

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.

Link: Qt: How to use QTimer to print a message to a QTextBrowser every 10 seconds?

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 QTextBrowser every 10 seconds. I was having trouble getting QTimer working at the time so I thought that if I could get QTimer to work then I could finish my task.

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.

Pseudocode:

while(not at the end of the file)
{
   read in 2500 lines 

   print a message

   wait 10 seconds
}

QTimer 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.

So to get it to work the way I want I first called the printMessage() SLOT and then I made a SLOT called stopTimer() that simply stops the timer. So after the 10 seconds has passed it will simply call stopTimer() and then continue processing the input file.

Onto the REAL problem:

Qt does not wait for a QTimer 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 QTimer has an isActive() function that returns a bool value.

So in the place where I wanted the 10 second delay to finish I put the following:

while(timer->isActive());

I figured the program would stay in this loop for 10 seconds and then exit after the QTimer 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 isActive( ) remains true regardless of elapsed time.

I then omitted the while(timer->isActive()) 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 while(timer->isActive()) 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 QTimer 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.

Someone please test this out or let me know a workaround for this!

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.

Here is an excerpt from code I have which currently freezes as stated above:

void Form::startBtn_pushed()
{
    QTimer *timer = new QTimer(this);
    QFile file(“file.txt”);
    QTextStream stream(&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->start(10000);

          while(timer->isActive()); //Wait for timer to timeout before continuing
       }
    }
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:35:39+00:00Added an answer on May 19, 2026 at 3:35 pm

    I have figured out a solution that is much simpler than those proposed here.

    I realized it was much easier to use a QTime object instead of a QTimer object.

    Basically you start a QTime object and then you use its elapsed() function to check how much time has passed to since it was started.

    Thanks to everyone for taking the time to help. I did try to implement a couple of your solutions into my code but had some trouble since I am not a pro at Qt and they were more complex than I would have thought. In the end I find this solution to be a easy solution to what should have been an easy problem.

    I certainly learned a lot from this problem and I hope you all did too.

    My question to you is why didn’t anyone suggest using QTime from the start?

    Thanks again!

    My solution:

        void Form::startBtn_pushed()
        {
            QTime *timer = new QTime();
            QFile file(“file.txt”);
            QTextStream stream(&file);
            QString line;
            int lineCount = 0;
    
            connect(timer, SIGNAL(timeout()), this, SLOT(stopTimer()));
    
            while(!(stream.atEnd())
            {
               line = stream.readLine();
               lineCount++;
    
               if(lineCount == 2500)
               { 
                  timer->start();
    
                  while(1)
                  {
                     QApplication::processEvents();
    
                     if(timer->elapsed() == 10000)
                     {
                        printMessage();
                        break;
                     }
    
                  }
               }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have question please refer the following code to understand the question. (I removed
Please refer to the following picture. I have a dataset called dsCompanyRecords and the
Please refer to the following image: I wanted to group the controls like that.
Please refer to the following fiddle: http://www.senchafiddle.com/#xXQlp Be sure to have your console open
Please refer the fiddle http://jsfiddle.net/HCqsM/5/ Here By clicking the 'click' link for the first
Please refer to this post. I have become able to configure my web.config file
I'm trying to split the following string primarily based on whitespace. Please refer to
I have a string like this: Please refer to document ABC.123.1234.1234 and document CBA.321.4321
I'm creating a bash script that will load CSV files using SQL*Loader. Please refer
Please refer to the following code: Filename: myclass.h @interface myclass:NSObject ... @end @interface NSObject(CategoryName)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.