Important: Please read the Qt Code of Conduct -

updating field in a modal dialog

  • Hi all -

    I've added a modal QDialog to display a progress bar for a file transfer. Updates to the progress bar would come from the worker thread to the main UI widget via a signal. The UI widget would in turn signal the progress QDialog to update the progress bar.

    How do I display the progress QDialog without blocking the UI Dialog from receiving signals from the worker? Do I have to make the progress QDialog modeless?


  • @mzimmers
    Does your "QDialog to display a progress bar" have any special "furniture" or other stuff on it you want requiting you to use your own QDialog, or could you use ? Because I think that shows example of using it modelessly, so it's written for you?

  • The only thing I was displaying besides the actual progress bar was the filename being transferred.

    I think I discovered the problem: I was invoking the progress QDialog with an exec() call. I changed it to open() and it seems to perform as desired. you have me interested in the QProgressDialog class. (I like that it has an active "cancel" button.)

    Here's what I'm trying:

        QProgressDialog qpd("Transferring file", "Abort Transfer", 0, 100, this);
        QObject::connect(this, &Widget::progressChanged, &qpd, &QProgressDialog::setValue);

    If I use the code as above, it blocks updates from the worker. If I change exec() to open(), it appears and immediately disappears. What might I be doing wrong? (I'm fine with this being a modal dialog.)


  • Lifetime Qt Champion


    qpd is a local variable (on the stack), therefore as soon a s you reach the end of the function, it's destroyed. You have to allocate it on the heap. However you have to also ensure that you destroy it properly once you're done with it.

  • @SGaist got it...thanks.

    // in main.cpp
    QObject::connect(worker, &Worker::transferInProgress, widget, &Widget::updateProgress); 
     QObject::connect(worker, &Worker::transferComplete, widget, &Widget::deleteProgressDialog);
    // in widget.h
    void updateProgress(int progress) {emit progressChanged(progress);}
    // in widget.cpp
    void Widget::showFileTransfer()
        m_fileTransferDialog = new QProgressDialog("Transferring file", "Abort Transfer", 0, 100, this);
        QObject::connect(this, &Widget::progressChanged, m_fileTransferDialog, &QProgressDialog::setValue);
    void Widget::deleteProgressDialog()
        delete m_fileTransferDialog;
        QMessageBox qmb(QMessageBox::NoIcon,
                        "File Transfer Complete",
                        "The file transfer has completed.",

Log in to reply