[Interest] question for a blocking background thread call

Elvis Stansvik elvstone at gmail.com
Wed Oct 21 20:53:26 CEST 2020


Den ons 21 okt. 2020 kl 09:08 skrev coroberti <coroberti at gmail.com>:
>
> 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 <QApplication>
#include <QPushButton>
#include <QProgressDialog>
#include <QMessageBox>
#include <QtConcurrentRun>
#include <QFuture>
#include <QFutureWatcher>

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<void>(this);

        connect(this, &TaskButton::progressChanged,
                progressDialog, &QProgressDialog::setValue);

        connect(watcher, &QFutureWatcher<void>::finished,
                watcher, &QFutureWatcher<void>::deleteLater);

        connect(watcher, &QFutureWatcher<void>::finished,
                progressDialog, &QProgressDialog::close);

        connect(watcher, &QFutureWatcher<void>::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 <elvstone at gmail.com> wrote:
> >
> > Den ons 21 okt. 2020 02:39Scott Bloom <scott at towel42.com> 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 <interest-bounces at qt-project.org> On Behalf Of Thiago Macieira
> >> Sent: Tuesday, October 20, 2020 3:44 PM
> >> To: interest at 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 at qt-project.org
> >> https://lists.qt-project.org/listinfo/interest
> >> _______________________________________________
> >> Interest mailing list
> >> Interest at qt-project.org
> >> https://lists.qt-project.org/listinfo/interest
> >
> > _______________________________________________
> > Interest mailing list
> > Interest at qt-project.org
> > https://lists.qt-project.org/listinfo/interest


More information about the Interest mailing list