[Interest] question for a blocking background thread call
Im working with a thirdparty lib, that I have zero control over 😊 Essentially, on a “button click” a long running blocking function call is executed. I can safely call the function into a background thread, but I don’t want the gui flow to continue. My idea is the following, a class that takes a lambda. When a blocking function is called on the class, the lambda is executed in a background thread. While the function waits calling processEvents without user events being handled (this part is easy…) Does anyone have an example to follow for this. Unfortunately, Im hitting all sorts of weirdness when getting this. TIA Scott ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
On 20/10/2020 22:16, Scott Bloom wrote: Essentially, on a “button click” a long running blocking function call is executed. I can safely call the function into a background thread, but I don’t want the gui flow to continue. My idea is the following, a class that takes a lambda. When a blocking function is called on the class, the lambda is executed in a background thread. While the function waits calling processEvents without user events being handled (this part is easy…) Does anyone have an example to follow for this. Unfortunately, Im hitting all sorts of weirdness when getting this. Can't you just create a QDialog and exec() it? Thanks, -- Giuseppe D'Angelo | giuseppe.dang...@kdab.com | Senior Software Engineer KDAB (France) S.A.S., a KDAB Group company Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com KDAB - The Qt, C++ and OpenGL Experts smime.p7s Description: S/MIME Cryptographic Signature ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
On Tuesday, 20 October 2020 14:26:10 PDT Giuseppe D'Angelo via Interest wrote: > Can't you just create a QDialog and exec() it? Or any other modal window in front. You probably want to display either a progress bar or a distraction, to let your users know that the application isn't frozen. -- Thiago Macieira - thiago.macieira (AT) intel.com Software Architect - Intel DPG Cloud Engineering ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
Well. I 100% totally overthought this... and was able to implement this in about 10 lines of code using a derivation of QProgressDIalog + QtConcurrent The progress dialog, is run with windowModality set to Qt::WindowModal, I overload exec, and launch the function then call QProgressDialog::exec. Works like charm. Thanks for the advice. Scott -Original Message- From: Interest On Behalf Of Thiago Macieira Sent: Tuesday, October 20, 2020 3:44 PM To: interest@qt-project.org Subject: Re: [Interest] question for a blocking background thread call On Tuesday, 20 October 2020 14:26:10 PDT Giuseppe D'Angelo via Interest wrote: > Can't you just create a QDialog and exec() it? Or any other modal window in front. You probably want to display either a progress bar or a distraction, to let your users know that the application isn't frozen. -- Thiago Macieira - thiago.macieira (AT) intel.com Software Architect - Intel DPG Cloud Engineering ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
Den ons 21 okt. 2020 02:39Scott Bloom skrev: > Well. I 100% totally overthought this... and was able to implement this in > about 10 lines of code using a derivation of QProgressDIalog + QtConcurrent > > The progress dialog, is run with windowModality set to Qt::WindowModal, > > I overload exec, and launch the function then call QProgressDialog::exec. > > Works like charm. Thanks for the advice. > Just to chime in: This is also the approach we've taken to some long running operations in our application, and it has also worked out fine. Elvis > Scott > -Original Message- > From: Interest On Behalf Of Thiago > Macieira > Sent: Tuesday, October 20, 2020 3:44 PM > To: interest@qt-project.org > Subject: Re: [Interest] question for a blocking background thread call > > On Tuesday, 20 October 2020 14:26:10 PDT Giuseppe D'Angelo via Interest > wrote: > > Can't you just create a QDialog and exec() it? > > Or any other modal window in front. You probably want to display either a > progress bar or a distraction, to let your users know that the application > isn't frozen. > > -- > Thiago Macieira - thiago.macieira (AT) intel.com > Software Architect - Intel DPG Cloud Engineering > > > > ___ > Interest mailing list > Interest@qt-project.org > https://lists.qt-project.org/listinfo/interest > ___ > Interest mailing list > Interest@qt-project.org > https://lists.qt-project.org/listinfo/interest > ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
Elvis and Scott, Could you please provide a code sample? Thanks, Kind regards, Robert On Wed, Oct 21, 2020 at 9:51 AM Elvis Stansvik wrote: > > Den ons 21 okt. 2020 02:39Scott Bloom skrev: >> >> Well. I 100% totally overthought this... and was able to implement this in >> about 10 lines of code using a derivation of QProgressDIalog + QtConcurrent >> >> The progress dialog, is run with windowModality set to Qt::WindowModal, >> >> I overload exec, and launch the function then call QProgressDialog::exec. >> >> Works like charm. Thanks for the advice. > > > Just to chime in: This is also the approach we've taken to some long running > operations in our application, and it has also worked out fine. > > Elvis > >> >> Scott >> -Original Message- >> From: Interest On Behalf Of Thiago Macieira >> Sent: Tuesday, October 20, 2020 3:44 PM >> To: interest@qt-project.org >> Subject: Re: [Interest] question for a blocking background thread call >> >> On Tuesday, 20 October 2020 14:26:10 PDT Giuseppe D'Angelo via Interest >> wrote: >> > Can't you just create a QDialog and exec() it? >> >> Or any other modal window in front. You probably want to display either a >> progress bar or a distraction, to let your users know that the application >> isn't frozen. >> >> -- >> Thiago Macieira - thiago.macieira (AT) intel.com >> Software Architect - Intel DPG Cloud Engineering >> >> >> >> ___ >> Interest mailing list >> Interest@qt-project.org >> https://lists.qt-project.org/listinfo/interest >> ___ >> Interest mailing list >> Interest@qt-project.org >> https://lists.qt-project.org/listinfo/interest > > ___ > Interest mailing list > Interest@qt-project.org > https://lists.qt-project.org/listinfo/interest ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
Den ons 21 okt. 2020 kl 09:08 skrev coroberti : > > Elvis and Scott, > Could you please provide a code sample? I can't show our actual code since it's proprietary, but here's an example: example.cpp: #include #include #include #include #include #include #include class TaskButton : public QPushButton { Q_OBJECT public: void startTask() { auto progressDialog = new QProgressDialog("Exporting...", QString(), 0, 10, this); progressDialog->setWindowModality(Qt::WindowModal); progressDialog->setAttribute(Qt::WA_DeleteOnClose); progressDialog->show(); auto future = QtConcurrent::run(this, &TaskButton::task); auto watcher = new QFutureWatcher(this); connect(this, &TaskButton::progressChanged, progressDialog, &QProgressDialog::setValue); connect(watcher, &QFutureWatcher::finished, watcher, &QFutureWatcher::deleteLater); connect(watcher, &QFutureWatcher::finished, progressDialog, &QProgressDialog::close); connect(watcher, &QFutureWatcher::finished, this, &TaskButton::finished); watcher->setFuture(future); } void task() { for (int progress = 1; progress < 11; ++progress) { QThread::sleep(1); // Heavy task emit progressChanged(progress); } } signals: void progressChanged(int progress); void finished(); }; int main(int argc, char *argv[]) { QApplication app(argc, argv); TaskButton button; button.setText("Start Task"); button.show(); QObject::connect(&button, &QPushButton::clicked, &button, &TaskButton::startTask); QObject::connect(&button, &TaskButton::finished, [&]() { QMessageBox::information(&button, "Finished!", "We're done!"); app.quit(); }); return app.exec(); } #include "example.moc" example.pro: TEMPLATE = app QT += widgets concurrent TARGET = example INCLUDEPATH += . DEFINES += QT_DEPRECATED_WARNINGS SOURCES += example.cpp HTH, Elvis > > Thanks, > > Kind regards, > Robert > > On Wed, Oct 21, 2020 at 9:51 AM Elvis Stansvik wrote: > > > > Den ons 21 okt. 2020 02:39Scott Bloom skrev: > >> > >> Well. I 100% totally overthought this... and was able to implement this in > >> about 10 lines of code using a derivation of QProgressDIalog + QtConcurrent > >> > >> The progress dialog, is run with windowModality set to Qt::WindowModal, > >> > >> I overload exec, and launch the function then call QProgressDialog::exec. > >> > >> Works like charm. Thanks for the advice. > > > > > > Just to chime in: This is also the approach we've taken to some long > > running operations in our application, and it has also worked out fine. > > > > Elvis > > > >> > >> Scott > >> -Original Message- > >> From: Interest On Behalf Of Thiago > >> Macieira > >> Sent: Tuesday, October 20, 2020 3:44 PM > >> To: interest@qt-project.org > >> Subject: Re: [Interest] question for a blocking background thread call > >> > >> On Tuesday, 20 October 2020 14:26:10 PDT Giuseppe D'Angelo via Interest > >> wrote: > >> > Can't you just create a QDialog and exec() it? > >> > >> Or any other modal window in front. You probably want to display either a > >> progress bar or a distraction, to let your users know that the application > >> isn't frozen. > >> > >> -- > >> Thiago Macieira - thiago.macieira (AT) intel.com > >> Software Architect - Intel DPG Cloud Engineering > >> > >> > >> > >> ___ > >> Interest mailing list > >> Interest@qt-project.org > >> https://lists.qt-project.org/listinfo/interest > >> ___ > >> Interest mailing list > >> Interest@qt-project.org > >> https://lists.qt-project.org/listinfo/interest > > > > ___ > > Interest mailing list > > Interest@qt-project.org > > https://lists.qt-project.org/listinfo/interest ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest
Re: [Interest] question for a blocking background thread call
Dear Elvis, It's very nice. Thanks a lot. Kind regards, Robert On Wed, Oct 21, 2020 at 9:53 PM Elvis Stansvik wrote: > > Den ons 21 okt. 2020 kl 09:08 skrev coroberti : > > > > Elvis and Scott, > > Could you please provide a code sample? > > I can't show our actual code since it's proprietary, but here's an example: > > example.cpp: > > #include > #include > #include > #include > #include > #include > #include > > class TaskButton : public QPushButton > { > Q_OBJECT > > public: > void startTask() { > auto progressDialog = new QProgressDialog("Exporting...", > QString(), 0, 10, this); > progressDialog->setWindowModality(Qt::WindowModal); > progressDialog->setAttribute(Qt::WA_DeleteOnClose); > progressDialog->show(); > > auto future = QtConcurrent::run(this, &TaskButton::task); > auto watcher = new QFutureWatcher(this); > > connect(this, &TaskButton::progressChanged, > progressDialog, &QProgressDialog::setValue); > > connect(watcher, &QFutureWatcher::finished, > watcher, &QFutureWatcher::deleteLater); > > connect(watcher, &QFutureWatcher::finished, > progressDialog, &QProgressDialog::close); > > connect(watcher, &QFutureWatcher::finished, > this, &TaskButton::finished); > > watcher->setFuture(future); > } > > void task() { > for (int progress = 1; progress < 11; ++progress) { > QThread::sleep(1); // Heavy task > emit progressChanged(progress); > } > } > > signals: > void progressChanged(int progress); > void finished(); > }; > > int main(int argc, char *argv[]) { > QApplication app(argc, argv); > > TaskButton button; > button.setText("Start Task"); > button.show(); > > QObject::connect(&button, &QPushButton::clicked, > &button, &TaskButton::startTask); > > QObject::connect(&button, &TaskButton::finished, [&]() { > QMessageBox::information(&button, "Finished!", "We're done!"); > app.quit(); > }); > > return app.exec(); > } > > #include "example.moc" > > > example.pro: > > TEMPLATE = app > QT += widgets concurrent > TARGET = example > INCLUDEPATH += . > DEFINES += QT_DEPRECATED_WARNINGS > SOURCES += example.cpp > > > HTH, > Elvis > > > > > Thanks, > > > > Kind regards, > > Robert > > > > On Wed, Oct 21, 2020 at 9:51 AM Elvis Stansvik wrote: > > > > > > Den ons 21 okt. 2020 02:39Scott Bloom skrev: > > >> > > >> Well. I 100% totally overthought this... and was able to implement this > > >> in about 10 lines of code using a derivation of QProgressDIalog + > > >> QtConcurrent > > >> > > >> The progress dialog, is run with windowModality set to Qt::WindowModal, > > >> > > >> I overload exec, and launch the function then call QProgressDialog::exec. > > >> > > >> Works like charm. Thanks for the advice. > > > > > > > > > Just to chime in: This is also the approach we've taken to some long > > > running operations in our application, and it has also worked out fine. > > > > > > Elvis > > > > > >> > > >> Scott > > >> -Original Message- > > >> From: Interest On Behalf Of Thiago > > >> Macieira > > >> Sent: Tuesday, October 20, 2020 3:44 PM > > >> To: interest@qt-project.org > > >> Subject: Re: [Interest] question for a blocking background thread call > > >> > > >> On Tuesday, 20 October 2020 14:26:10 PDT Giuseppe D'Angelo via Interest > > >> wrote: > > >> > Can't you just create a QDialog and exec() it? > > >> > > >> Or any other modal window in front. You probably want to display either > > >> a progress bar or a distraction, to let your users know that the > > >> application isn't frozen. > > >> > > >> -- > > >> Thiago Macieira - thiago.macieira (AT) intel.com > > >> Software Architect - Intel DPG Cloud Engineering > > >> > > >> > > >> > > >> ___ > > >> Interest mailing list > > >> Interest@qt-project.org > > >> https://lists.qt-project.org/listinfo/interest > > >> ___ > > >> Interest mailing list > > >> Interest@qt-project.org > > >> https://lists.qt-project.org/listinfo/interest > > > > > > ___ > > > Interest mailing list > > > Interest@qt-project.org > > > https://lists.qt-project.org/listinfo/interest ___ Interest mailing list Interest@qt-project.org https://lists.qt-project.org/listinfo/interest