Rajeshwar
Rajeshwar

Reputation: 11651

Updating GUI form elements continuously so that Form does not hang

As the title states I am attempting to update a GUI form element continuously via a thread however the form still seems to be busy. This I cannot understand.Here is how I am approaching it.

connect(this,SIGNAL(SIGUpdateForm),this,SLOT(MyUpdateMehtod));

now whenever the form needs to be updated I simply do the following.I launch a method in a new thread . The new thread then triggers the above signal.

boost::thread t(&SomeClass::SomeMethod(),this);

Now once someMethod is started here is what I do

void SomeMethod()
{
      SIGUpdateForm(); //Launch the signal that will update the form
}

The SIGUpdateForm then calls MyUpdateMehtod() however since signal (whether queued or direct do not launch any thread thus it seems like the form is hung.) But this confuses me because the signal itself is being called from an independent thread so why is the form hanging ? What can i do to make this work ?

Upvotes: 0

Views: 692

Answers (1)

Md. Minhazul Haque
Md. Minhazul Haque

Reputation: 638

Qt has its own thread. Actually you don't need a thread. QTimer will do it for you. Here is an example.

void updateForm()
{
 ui->bla->setText("bla");
 // bla bla method
}

QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(updateForm()));
timer.start(3000);

Now updateForm() will be called every 3s. GUI will not hang. Another way of doing so is processing the event loop,

while(....)
{
 // some lengthy task
 qApp->processEvents(QEventLoop::AllEvents);
}

Upvotes: 1

Related Questions