c++ - Avoiding GUI freezing on multithreaded operations -
i have qt gui class preferenceswindow
that, obviously, responsible handling user preferences. have fields manage connection database server. when field left, dbschanged()
method called. below code managed write:
void preferenceswindow::dbschanged() { qfuture<qstringlist> loader = run(this, &preferenceswindow::get_databases); qstringlist databases = loader.result(); if (databases.length()) { this->ui.database->show(); this->ui.nodb_label->hide(); this->ui.database->clear(); this->ui.database->additems(databases); this->ui.okbutton->setdisabled(false); this->ui.validationstatus->setpixmap(qpixmap(":/icon/tick.png")); } else { this->ui.database->hide(); this->ui.nodb_label->show(); this->ui.okbutton->setdisabled(true); this->ui.validationstatus->setpixmap(qpixmap(":/icon/error.png")); } } qstringlist preferenceswindow::get_databases() { qsqldatabase test_connection; if (qsqldatabase::contains("preferemces_live_test_connection")) test_connection = qsqldatabase::database("preferemces_live_test_connection"); else test_connection = qsqldatabase::adddatabase("qmysql", "preferemces_live_test_connection"); test_connection.sethostname(this->ui.serveraddress->text()); test_connection.setusername(this->ui.username->text()); test_connection.setpassword(this->ui.password->text()); test_connection.setdatabasename(this->ui.database->currenttext()); test_connection.setport(this->ui.serverport->value()); test_connection.open(); qdebug() << "error: " << test_connection.lasterror(); qsqlquery show_databases = test_connection.exec("show databases"); qstringlist databases; while (show_databases.next()) { databases.append(show_databases.value(0).tostring()); } qsqldatabase::removedatabase("preferences_live_test_connection"); return databases; }
since get_databases
can take long time, thought putting in on separate thread can see in these 2 lines:
qfuture<qstringlist> loader = run(this, &preferenceswindow::get_databases); qstringlist databases = loader.result();
could solve problem. runs on separate thread, still freezes gui (while working).
how should rewrite entire process? though of solutions, not sure performance, , don't want work uselessly...
it freezes gui because though get_databases
call in separate thread, still wait results causes freeze.
i don't know how in qt, normal thing open dialog saying "please wait" or cancel button, , have worker thread send signal parent (gui) thread when done.
Comments
Post a Comment