PDA

View Full Version : How to use progressTextChanged() signal in QFutureWatcher



DiamonDogX
9th June 2009, 15:36
I have a task I want to run upon clicking a button. I want the method to be run in a separate thread so as not to hang up the GUI while it is running (the GUI does indeed hang with my current code below). I also want the method running the task to report back text information to be appended to a TextEdit control, e.g. "Starting task...", "Reading file...", "Performing computation...", etc. I've set up a QFuture, QFutureWatcher, etc. but not sure how to actually emit the progressTextChanged signal and not sure why GUI is hanging:


//...
//...
QFuture<bool> future = QtConcurrent::run(
this,
&MyClass::runTheTask );

QFutureWatcher<bool> watcher;
connect( &watcher, SIGNAL( progressTextChanged( const QString ) ),
this, SLOT( appendMessageToStatusLineEdit( const QString ) ) );
watcher.setFuture( future );
future.waitForFinished();

// ...
// ...


bool MyClass::runTheTask()
{
// emit progress text signal???

// currently I have something like the following:
appendMessageToStatusLineEdit( "Status update..." ); // don't see this happen until the entire task is complete for some reason
// more code
// etc.
}

void MyClass::appendMessageToStatusLineEdit( const QString msg )
{
QMetaObject::invokeMethod( this,
"doAppendMessageToStatusLineEdit",
Qt::QueuedConnection,
Q_ARG( QString, msg ) );
}

void MyClass::doAppendMessageToStatusLineEdit( const QString msg )
{
_ui->statusTextEdit->append( msg );
}

wysota
9th June 2009, 15:41
It hangs because you call waitForFinished().