Modal QProgressDialog updated from worker thread

1.7k views Asked by At

I want to update a modal QProgressDialog from my worker thread. However, if I set the dialog to be modal, my application crashes (and the dialog did not show any progress). If I do not, everything goes fine (but the user can tinker around with the rest of the program, which may cause issues).

What am I doing wrong?

Minimum code sample follows:

filereader qfr;    

QProgressDialog progress("Importing file.", "Cancel", 0, file_size);

connect(&qfr, &filereader::signalProgress, &progress, &QProgressDialog::setValue, Qt::QueuedConnection);

QThread worker_thread;

std::atomic<bool> success = false;

connect(&worker_thread, &QThread::started,
        [&]() {
    success = qfr.read_file(/* parameters */);
    worker_thread.quit();});

worker_thread.start();

//progress.setWindowModality(Qt::WindowModal); // Works only fine when this line is commented !!

while (worker_thread.isRunning()) {
    QApplication::processEvents();
    QThread::sleep(0);
}

progress.close();
1

There are 1 answers

0
thuga On

Your thread is pretty much pointless. It serves no real purpose. You could have as well just called QApplication::processEvents in your read_file method. But you shouldn't, calling processEvents is bad practice.

What you should do is remove that while loop, and make your progress dialog a member of your class. I don't really like how that lambda looks either. I would personally just use filereader::read_file as a slot.

Note that Qt::windowModal blocks input to the parent window. Your progress dialog has no parent. So you would either have to call progress->setModal(true), or progress.setWindowModality(Qt::ApplicationModal);. Or set a parent to it.

Here is a small example (it is not tailor made for your application, but it should point you in the right direction):

#include <QtWidgets>

class Worker : public QObject
{
    Q_OBJECT
public:
    Worker(QObject *parent = nullptr) : QObject(parent){}
public slots:
    void simulateLongProcess()
    {
        for(int i = 0; i < 101; i++)
        {
            emit progressChanged(i);
            QThread::msleep(100);
        }
        emit finishedWorking(true);
    }
signals:
    void progressChanged(int progress);
    void finishedWorking(bool result);
};

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr) : QWidget(parent)
    {
        setLayout(new QHBoxLayout);
        progress_dialog.setModal(true);
        progress_dialog.setAutoReset(false);
        progress_dialog.setCancelButton(nullptr);
        QThread *thread = new QThread(this);
        connect(thread, &QThread::started, &worker, &Worker::simulateLongProcess);
        connect(&worker, &Worker::finishedWorking, thread, &QThread::quit);
        connect(&worker, &Worker::progressChanged, &progress_dialog, &QProgressDialog::setValue);
        connect(&worker, &Worker::finishedWorking, &progress_dialog, &QProgressDialog::close);
        connect(&worker, &Worker::finishedWorking, this, &Widget::handleResult);
        QPushButton * start_button = new QPushButton("START");
        connect(start_button, &QPushButton::clicked, this, [=]
        {
            progress_dialog.show();
            thread->start();
        });
        layout()->addWidget(start_button);
        resize(400, 300);
    }

public slots:
    void handleResult(bool result)
    {
        // do something with the result
    }

private:
    QProgressDialog progress_dialog;
    Worker worker;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

#include "main.moc"