While this works, I have this strange feeling my QObject emit is not threadsafe and the fact that this hasn't exploded yet is just luck.
void LoginController::attemptLogin(QString username, QString password)
{
emit loginAttemptStatus(QString("Connecting to service..."));
QFuture<bool> future = QtConcurrent::run([&](QString username, QString password){
// fake a long running operation
QThread::sleep(1);
emit loginAttemptStatus(QString("Connected to service..."));
// a dumb test login
QString u("asdf");
bool success = username.compare( u ) == 0;
if ( success ) {
emit loginAttemptStatus(QString("Success..."));
} else {
emit loginAttemptStatus(QString("Failure..."));
}
return success;
}, username, password);
this->watchLoginAttempt.setFuture(future);
}
So, does capturing a reference to this
going to cause problems?
Because I think it is but I cannot find a definitive answer.
The only case when it can explode, is when you destroy
LoginController
before lambda expression ends. Proper handling ofwatchLoginAttempt
(future
) should prevent this.