Solved 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?
Thanks...
-
@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 ownQDialog
, or could you use http://doc.qt.io/qt-5/qprogressdialog.html ? 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. But...now 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); qpd.setAutoClose(false); qpd.exec();
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.)
Thanks.
-
Hi,
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); m_fileTransferDialog->open(); } ... void Widget::deleteProgressDialog() { delete m_fileTransferDialog; QMessageBox qmb(QMessageBox::NoIcon, "File Transfer Complete", "The file transfer has completed.", QMessageBox::Ok, this); qmb.exec(); }