[clug] long run function in trolltech qt widget

Jason Stokes glasper9 at yahoo.com.au
Thu Dec 4 20:50:08 GMT 2008

Hi jm,

I'm certainly not an expert in qt, but this is a standard
"perform a background task in an event-driven application" problem.  As
Brad has noted, while you are executing "long run" the event processing
loop of your application is not being executed (or returned to) and
hence you won't see any application updates until /after/ long run has
finished.  All the events are being queued; when you return, they all
get processed in one big whump.

You have essentially three choices, in order of work:

Modify "callback" to invoke the QApplication::processEvents() function,
which will process the outstanding events in the event queue.  However
the googling I've done suggests this is unsafe in general.
Modify "long_run" to do its job in stoppable and startable increments,
then call "long_run" from a QTimer tick event.  Each time "long_run" is
invoked, it should do a small amount of work, then save its state and
return, waiting for the timer event to occur again.  Event processing
will happen each time you return from the timer callback.
- Use a seperate thread (or processs) to execute "long-run."  Now you have all the problems of seperate threads.

Here's a quick link:

----- Original Message ----
From: jm <jeffm at ghostgun.com>
To: linux at lists.samba.org
Sent: Thursday, 4 December, 2008 9:50:11 AM
Subject: Re: [clug] long run function in trolltech qt widget

Brad Hards wrote:
> The normal approach would be to connect the accepted() signal to a custom slot that executes long_run().
> However to show any GUI stuff changing, you need to return to the event loop. How is long_run() returning control to the event loop? Are you dividing up the run into multiple steps, or are using some kind of threading approach?

I have code similar to,

MyPage::MyPage(QWidget *parent)
: QWizardPage(parent)
setTitle(tr("Do the work"));
progress = new QProgressBar(this);

extern "C" void callback(void *arg)
QProgressBar *bar = (QProgressBar *)arg;

printf("doing work\");
bar->setValue(bar->value() + 1);

  result = long_run(callback,  (void *) progress)

  // To be done
  return true;

doWork() doesn't return until it's actually finished the calculation. There isn't a contiuation unfortunately. How do I call doWork() so that MyPage is displayed while the work is being done? I've been trying to avoid a thread for this as it only meant to be a prototype at this stage and QThread looks as if it will take a while to understand. Especially getting values in and out of threads.


-- linux mailing list
linux at lists.samba.org

      Start your day with Yahoo!7 and win a Sony Bravia TV. Enter now http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline

More information about the linux mailing list