single QProgressDialog button push produces two signals
-
Hi all -
m_fileTransferDialog = new QProgressDialog("Transferring file", "Abort Transfer", 0, 100, this); QObject::connect(m_fileTransferDialog, &QProgressDialog::canceled, this, &Widget::relayTransferCanceled); m_fileTransferDialog->open();I press the cancel button and get two calls to my slot. What might I be doing wrong?
-
Hi,
Does that code get called several times ?
Do you properly deletem_fileTransferDialogafter use or before calling that part ? -
I only call that code once (per file transfer).
I hadn't been destroying the object, but now I do (same results). Here's the slot:
void Widget::relayTransferCanceled() { emit fwUpdateCanceled(); m_fileTransferDialog->close(); delete m_fileTransferDialog; QMessageBox qmb(QMessageBox::NoIcon, "File Transfer Canceled", "The file transfer has been canceled.", QMessageBox::Ok, this); } -
I only call that code once (per file transfer).
I hadn't been destroying the object, but now I do (same results). Here's the slot:
void Widget::relayTransferCanceled() { emit fwUpdateCanceled(); m_fileTransferDialog->close(); delete m_fileTransferDialog; QMessageBox qmb(QMessageBox::NoIcon, "File Transfer Canceled", "The file transfer has been canceled.", QMessageBox::Ok, this); }Useshow(), notopen().Disregard, I must've had a brain aneurysm or something.
-
Yeah, I was wondering how that was going to make a difference. The QDialog documentation doesn't really go into the difference between show() and open(), but they look almost identical in this use instance.
@mzimmers said in single QProgressDialog button push produces two signals:
The QDialog documentation doesn't really go into the difference between show() and open(), but they look almost identical in this use instance.
The latter is just
show()for a modal dialog (i.e. it callssetModal(true)for you). I had that anti-epiphany due toQProgress's ownopen()overload, which allows you to bind a function pointer to one of the dialog's signals. Anyway.
Is it possible you have created 2 dialogs and due to having two objects emitting the signal you get to the slot twice? That'd be the most "obvious" thing I'd check as you're holding objects as members and it happens to us all (unfortunately) to forget having an object and just replacing the pointer member variable without realizing. -
@mzimmers said in single QProgressDialog button push produces two signals:
m_fileTransferDialog
You only delete the QProgressDialog when it's canceled, at least in the show you've shown us.
-
@mzimmers said in single QProgressDialog button push produces two signals:
m_fileTransferDialog
You only delete the QProgressDialog when it's canceled, at least in the show you've shown us.
@Christian-Ehrlicher good point, and I'll correct that. I don't think it has any bearing on this problem though, as this issue occurs the first time through the code.
-
@Christian-Ehrlicher good point, and I'll correct that. I don't think it has any bearing on this problem though, as this issue occurs the first time through the code.
@mzimmers
If you sayrelayTransferCanceled()is called twice, what is the value ofm_fileTransferDialogsecond time? You can put a breakpoint to see it hittingdelete m_fileTransferDialog;. So really it should crash on the second time (on them_fileTransferDialog->close();), unless the calls have quite distinctm_fileTransferDialog;s? -
@mzimmers
If you sayrelayTransferCanceled()is called twice, what is the value ofm_fileTransferDialogsecond time? You can put a breakpoint to see it hittingdelete m_fileTransferDialog;. So really it should crash on the second time (on them_fileTransferDialog->close();), unless the calls have quite distinctm_fileTransferDialog;s?@JonB that's exactly what it does -- crash on the second close() attempt.
In looking at the canceled() signal, I wonder if I should disconnect it from the default slot cancel() before I use it. I notice that closing the window also triggers the signal.
EDIT:
Tried the above, no help. I'm definitely doing something to cause this code to execute twice, but I sure don't know what it is. Here's the latest effort:
void Widget::showFileTransfer() { m_fileTransferDialog = new QProgressDialog("Transferring file", "Abort Transfer", 0, 100, this); QObject::connect(this, &Widget::progressChanged, m_fileTransferDialog, &QProgressDialog::setValue); QObject::connect(m_fileTransferDialog, &QProgressDialog::canceled, this, &Widget::relayTransferCanceled); m_fileTransferDialog->show(); } void Widget::relayTransferCanceled() { emit transferCanceled(); deleteProgressDialog(false);void Widget::deleteProgressDialog(bool success) } void Widget::deleteProgressDialog(bool success) { QString title; QString text; if (success) { title = "File Transfer Complete"; text = "The file transfer has completed."; } else { title = "File Transfer Did Not Complete"; text = "The file transfer did not complete successfully."; } QMessageBox *qmb; qmb = new QMessageBox(QMessageBox::NoIcon, title, text, QMessageBox::Ok, this); qmb->exec(); m_fileTransferDialog->close(); delete m_fileTransferDialog; delete qmb; } -
How many file transfert can you have ?
If several, you should reconsider your dialog and give it the capability to show information for more than one transfert.
-
@JonB that's exactly what it does -- crash on the second close() attempt.
In looking at the canceled() signal, I wonder if I should disconnect it from the default slot cancel() before I use it. I notice that closing the window also triggers the signal.
EDIT:
Tried the above, no help. I'm definitely doing something to cause this code to execute twice, but I sure don't know what it is. Here's the latest effort:
void Widget::showFileTransfer() { m_fileTransferDialog = new QProgressDialog("Transferring file", "Abort Transfer", 0, 100, this); QObject::connect(this, &Widget::progressChanged, m_fileTransferDialog, &QProgressDialog::setValue); QObject::connect(m_fileTransferDialog, &QProgressDialog::canceled, this, &Widget::relayTransferCanceled); m_fileTransferDialog->show(); } void Widget::relayTransferCanceled() { emit transferCanceled(); deleteProgressDialog(false);void Widget::deleteProgressDialog(bool success) } void Widget::deleteProgressDialog(bool success) { QString title; QString text; if (success) { title = "File Transfer Complete"; text = "The file transfer has completed."; } else { title = "File Transfer Did Not Complete"; text = "The file transfer did not complete successfully."; } QMessageBox *qmb; qmb = new QMessageBox(QMessageBox::NoIcon, title, text, QMessageBox::Ok, this); qmb->exec(); m_fileTransferDialog->close(); delete m_fileTransferDialog; delete qmb; }@mzimmers said in single QProgressDialog button push produces two signals:
@JonB that's exactly what it does -- crash on the second close() attempt.
Aha! OK, you didn't say that :)
So I presume when you hit each time the stacktraces don't reveal anything different?
-
@mzimmers said in single QProgressDialog button push produces two signals:
@JonB that's exactly what it does -- crash on the second close() attempt.
Aha! OK, you didn't say that :)
So I presume when you hit each time the stacktraces don't reveal anything different?
-
Sorry about that. The stack quickly enters Qt space, so I can't do much with it myself. Interesting, though, that the 2nd iteration shows my code on level 17 whereas the 1st one doesn't.
@mzimmers
So the second call to the slot is coming from yourWidget::deleteProgressDialog()on linem_fileTransferDialog->close();I know nothing about this, but does closing the dialog cause the
QProgressDialog::canceledsignal to be raised? Does unconnecting that signal prior tom_fileTransferDialog->close();solve your problem? take a look at https://www.qtcentre.org/threads/53056-QProgressDialog-cancel-signal-is-emitted-always ?Also, you are calling
deleteProgressDialog()in yourrelayTransferCanceled()slot. Should you/is it safe to be goingm_fileTransferDialog->close(); delete m_fileTransferDialog;from a slot? Should it be more link
deleteLater()?These are just thoughts for you! I know nothing... :)
-
Hi Jon - all good suggestions, and all appreciated. I don't know of any reason I can't delete from a slot, but I did convert it to deleteLater().
I thought I'd already tried your suggestion to use disconnect() without success, but I tried it again, and now it seems to work.
void Widget::showFileTransfer() { m_fileTransferDialog = new QProgressDialog("Transferring file", "Abort Transfer", 0, 100, this); QObject::connect(this, &Widget::progressChanged, m_fileTransferDialog, &QProgressDialog::setValue); QObject::connect(m_fileTransferDialog, &QProgressDialog::canceled, this, &Widget::relayTransferCanceled); m_fileTransferDialog->show(); } void Widget::relayTransferCanceled() { QObject::disconnect(m_fileTransferDialog, &QProgressDialog::canceled, nullptr, nullptr); emit transferCanceled(); deleteProgressDialog(FILE_TRANSFER_CANCELED); }I'd like to get to the bottom of this issue, so I'm going to leave it as unsolved for a few days, but at least I have a workaround. Thanks.
-
I think I may have found at least part of the problem. It seems that my call to close() emits the canceled() signal, which was getting me in trouble, as my slot wasn't prepared for a signal under these circumstances. I guess I'm not supposed to call close(), which is actually a slot.
My app seems to be working now. I'm going to keep this unsolved for another day, just in case someone has something else to add.
So