[clug] long run function in trolltech qt widget
jm
jeffm at ghostgun.com
Thu Dec 4 23:41:16 GMT 2008
Daniel Pittman wrote:
>
>> You could use a QThread (its not hard, but make sure you read the
>> docs).
>>
>
> Hah. This, to me, seems like a rather risky statement: the original
> poster stated that this was a wrapped C library, which starts us off on
> an exciting foot.
>
> Are you certain that the library doesn't make unsafe use of a signal
> handler? How about those non-entrant libc functions? Do /you/ want to
> explain appropriate locking to protect shared data structures?
>
Luckily this assumption holds. I got the thread version going yesterday,
but the problem now is that it runs multiple times. The thread start
method it called from within the wizard's page showevent() method, but
it seems that in the course of things it receives multiple QEvent::Show
events. I've tried various way of having it run only once, but seem to
be missing something. At the moment the code is something similar to
// The GenPage initialiser among other things does...
newval = NULL;
complete = false;
genThread = new GenThread();
// QThread emits finished when run() completes
connect(genThread, SIGNAL(finished()), this, SIGNAL(completeChanged()));
// tick is emitted by GenThread everytime the callback is called
connect(genThread, SIGNAL(tick()), this,
SLOT(incrementProgressBar()));
void GenPage::generate()
{
progress->setValue(0);
if (!genThread->isRunning())
// create new thread to do work
genThread->start();
}
bool GenPage::isComplete()
{
bool curComplete = complete;
complete = genThread->isFinished();
std::cout << "isComplete:" << complete << std::endl;
if (curComplete != complete)
emit completeChanged();
return complete;
}
void GenPage::incrementProgressBar()
{
progress->setValue(progress->value() + 1);
}
void GenPage::showEvent ( QShowEvent * event )
{
std::cout << int(event->type()) << std::endl;
if (!complete)
generate();
}
It's looking at is showEvent is incorrect or that that generate should
be called from somewhere else. Thoughts?
Jeff.
More information about the linux
mailing list