" QDialog::Accepted" is doesn't work.
-
this doesn't work.
As far as I know, clicking the OK button fires the Accepted signal.
Then the following function should work.if (dlg->exec() == QDialog::Accepted) { dlg->close(); }
But this doesn't work.
Is there something I'm misunderstanding?
Need something else?ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->show(); if (dlg->exec() == QDialog::Accepted) { dlg->close(); }
-
@JonB said in " QDialog::Accepted" is doesn't work.:
Do you know that his exec() does return?
No, it does not. At least not from the code we can see. Maybe when the progress bar hits 100%, you never know :)
@MyNameIsQt
show()
+exec()
is also redundant and these are two different approaches.
show()
just shows the widget / makes it visible, whileexec()
creates its own, blocking event loop.To call your dialog, you dont need more than this.
ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->open();
and in your dialog code something like
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
or play with the
QDialogButtonRoles
( https://doc.qt.io/qt-6/qdialogbuttonbox.html#StandardButton-enum)@JonB, I just realized that the connect isn't necessarily needed. If these buttons are
StandardButtons
likeQDialogButtonBox::Ok
, which already has theAcceptRole
and therefore triggersQDialog::Accept
on click.@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@JonB just realized that the connect isn't necessarily needed. If these buttons are StandardButtons like QDialogButtonBox::Ok, which already has the AcceptRole and therefore triggers QDialog::Accept on click.
Then please explain why https://doc.qt.io/qt-6/qdialogbuttonbox.html#details has the following explanation plus code:
Alternatively, QDialogButtonBox provides several standard buttons (e.g. OK, Cancel, Save) that you can use. They exist as flags so you can OR them together in the constructor.
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
? TBH I am getting a bit tired of answering the same each time. I have already said that
QDialogButtonBox::accepted
is a dialogbuttonbox signal, and is not the same asQDialog::accepted()
(signal) orQDialog::accept()
(slot), which is why I typed it all in. Unless you know better. -
this doesn't work.
As far as I know, clicking the OK button fires the Accepted signal.
Then the following function should work.if (dlg->exec() == QDialog::Accepted) { dlg->close(); }
But this doesn't work.
Is there something I'm misunderstanding?
Need something else?ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->show(); if (dlg->exec() == QDialog::Accepted) { dlg->close(); }
@MyNameIsQt
You show you have aQDialogButtonBox
. Which I assume either defaults, or you have set, to have OK and Cancel buttons. But where have you set theconnect()
so that they callaccept
/reject()
on the dialog?As far as I know, clicking the OK button fires the Accepted signal.
It fires
QDialogButtonBox::accepted()
signal, that is NOT the same thing as eitherQDialog::accepted()
(signal) orQDialog::accept()
(slot).You should not need
dlg->show();
nordlg->close()
once you have it working. -
@MyNameIsQt
You show you have aQDialogButtonBox
. Which I assume either defaults, or you have set, to have OK and Cancel buttons. But where have you set theconnect()
so that they callaccept
/reject()
on the dialog?As far as I know, clicking the OK button fires the Accepted signal.
It fires
QDialogButtonBox::accepted()
signal, that is NOT the same thing as eitherQDialog::accepted()
(signal) orQDialog::accept()
(slot).You should not need
dlg->show();
nordlg->close()
once you have it working.@JonB said in " QDialog::Accepted" is doesn't work.:
You should not need dlg->show(); nor dlg->close() once you have it working.
@MyNameIsQt this is the most important part :)
Once aQDialog
finishes with anyDialogCode
, there's no need toclose()
it seperately.void QDialog::reject() Hides the modal dialog and sets the result code to Rejected.
( https://doc.qt.io/qt-6/qdialog.html#reject )
void QDialog::accept() Hides the modal dialog and sets the result code to Accepted.
( https://doc.qt.io/qt-6/qdialog.html#accept)
In addition, better use
open()
overshow()
orexec()
as is also stated hereEdit:
From the image on the right, one can see, that you don't use layouts in your GUI (note the red crossed circle symbol next to your widgets). Do it :) It helps a lot managing your content.
-
@JonB said in " QDialog::Accepted" is doesn't work.:
You should not need dlg->show(); nor dlg->close() once you have it working.
@MyNameIsQt this is the most important part :)
Once aQDialog
finishes with anyDialogCode
, there's no need toclose()
it seperately.void QDialog::reject() Hides the modal dialog and sets the result code to Rejected.
( https://doc.qt.io/qt-6/qdialog.html#reject )
void QDialog::accept() Hides the modal dialog and sets the result code to Accepted.
( https://doc.qt.io/qt-6/qdialog.html#accept)
In addition, better use
open()
overshow()
orexec()
as is also stated hereEdit:
From the image on the right, one can see, that you don't use layouts in your GUI (note the red crossed circle symbol next to your widgets). Do it :) It helps a lot managing your content.
@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@MyNameIsQt this is the most important part :)
So far as I can see, this is not the important part :) It's only icing on the cake ---
exec()
will do both theshow()
&hide()
for you, though it's probably harmless to do them here too [actually with theQt::WA_DeleteOnClose
cannot afford to dodlg->anything()
afterexec()
returns].The "important part" is that OP has not connected slots to the buttons [I assume], so nowt happens regardless of the
show()
orclose()
.... -
@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@MyNameIsQt this is the most important part :)
So far as I can see, this is not the important part :) It's only icing on the cake ---
exec()
will do both theshow()
&hide()
for you, though it's probably harmless to do them here too [actually with theQt::WA_DeleteOnClose
cannot afford to dodlg->anything()
afterexec()
returns].The "important part" is that OP has not connected slots to the buttons [I assume], so nowt happens regardless of the
show()
orclose()
....@JonB said in " QDialog::Accepted" is doesn't work.:
So far as I can see, this is not the important part :)
Without the call of
close()
there would be no topic :)
Then OP would have had to figure out in another way, why (or that) the dialog never reachesAccepted
status through the buttonBox :) -
@JonB said in " QDialog::Accepted" is doesn't work.:
So far as I can see, this is not the important part :)
Without the call of
close()
there would be no topic :)
Then OP would have had to figure out in another way, why (or that) the dialog never reachesAccepted
status through the buttonBox :)@Pl45m4
I do not follow what you are saying at all. I do not think theclose()
is relevant. I do not think the OP'sdlg->exec()
returns, for the reason I have suggested and the remedy required. Do you know that hisexec()
does return?Of course if people would please not write
@MyNameIsQt said in " QDialog::Accepted" is doesn't work.:But this doesn't work.
and make readers have to guess what they might mean/see/not see, answering would be a lot easier....
-
@Pl45m4
I do not follow what you are saying at all. I do not think theclose()
is relevant. I do not think the OP'sdlg->exec()
returns, for the reason I have suggested and the remedy required. Do you know that hisexec()
does return?Of course if people would please not write
@MyNameIsQt said in " QDialog::Accepted" is doesn't work.:But this doesn't work.
and make readers have to guess what they might mean/see/not see, answering would be a lot easier....
@JonB said in " QDialog::Accepted" is doesn't work.:
Do you know that his exec() does return?
No, it does not. At least not from the code we can see. Maybe when the progress bar hits 100%, you never know :)
@MyNameIsQt
show()
+exec()
is also redundant and these are two different approaches.
show()
just shows the widget / makes it visible, whileexec()
creates its own, blocking event loop.To call your dialog, you dont need more than this.
ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->open();
and in your dialog code something like
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
or play with the
QDialogButtonRoles
( https://doc.qt.io/qt-6/qdialogbuttonbox.html#StandardButton-enum)@JonB, I just realized that the connect isn't necessarily needed. If these buttons are
StandardButtons
likeQDialogButtonBox::Ok
, which already has theAcceptRole
and therefore triggersQDialog::Accept
on click. -
@JonB said in " QDialog::Accepted" is doesn't work.:
Do you know that his exec() does return?
No, it does not. At least not from the code we can see. Maybe when the progress bar hits 100%, you never know :)
@MyNameIsQt
show()
+exec()
is also redundant and these are two different approaches.
show()
just shows the widget / makes it visible, whileexec()
creates its own, blocking event loop.To call your dialog, you dont need more than this.
ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->open();
and in your dialog code something like
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
or play with the
QDialogButtonRoles
( https://doc.qt.io/qt-6/qdialogbuttonbox.html#StandardButton-enum)@JonB, I just realized that the connect isn't necessarily needed. If these buttons are
StandardButtons
likeQDialogButtonBox::Ok
, which already has theAcceptRole
and therefore triggersQDialog::Accept
on click.@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@JonB just realized that the connect isn't necessarily needed. If these buttons are StandardButtons like QDialogButtonBox::Ok, which already has the AcceptRole and therefore triggers QDialog::Accept on click.
Then please explain why https://doc.qt.io/qt-6/qdialogbuttonbox.html#details has the following explanation plus code:
Alternatively, QDialogButtonBox provides several standard buttons (e.g. OK, Cancel, Save) that you can use. They exist as flags so you can OR them together in the constructor.
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
? TBH I am getting a bit tired of answering the same each time. I have already said that
QDialogButtonBox::accepted
is a dialogbuttonbox signal, and is not the same asQDialog::accepted()
(signal) orQDialog::accept()
(slot), which is why I typed it all in. Unless you know better. -
@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@JonB just realized that the connect isn't necessarily needed. If these buttons are StandardButtons like QDialogButtonBox::Ok, which already has the AcceptRole and therefore triggers QDialog::Accept on click.
Then please explain why https://doc.qt.io/qt-6/qdialogbuttonbox.html#details has the following explanation plus code:
Alternatively, QDialogButtonBox provides several standard buttons (e.g. OK, Cancel, Save) that you can use. They exist as flags so you can OR them together in the constructor.
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
? TBH I am getting a bit tired of answering the same each time. I have already said that
QDialogButtonBox::accepted
is a dialogbuttonbox signal, and is not the same asQDialog::accepted()
(signal) orQDialog::accept()
(slot), which is why I typed it all in. Unless you know better.@JonB said in " QDialog::Accepted" is doesn't work.:
I have already said that QDialogButtonBox::accepted is a dialogbuttonbox signal, and is not the same as QDialog::accepted() (signal) or QDialog::accept() (slot), which is why I typed it all in. Unless you know better.
I dont know better, but I just checked the documentation and found this :)
[Edit:QDialogButtonBox
signal mistaken forQDialog
]When a button is clicked in the button box, the clicked() signal is emitted for the actual button is that is pressed. For convenience, if the button has an AcceptRole, RejectRole, or HelpRole, the accepted(), rejected(), or helpRequested() signals are emitted respectively.
( https://doc.qt.io/qt-6/qdialogbuttonbox.html#details)
And AFAIK these Roles are applied automatically if you create a buttonBox in Designer.
[ Edit: Yes, but only the buttonBox ones... :) ]I'm also confused right now :D
BTW: @MyNameIsQt sorry for discussing in your topic :)
If you need theif (dlg->exec() == QDialog::Accepted)
-check only to callclose()
, you can remove it.
Then it's up to you whether you move fromexec()
todlg->open()
.
After that, check if the signals are still not emitted. -
@JonB said in " QDialog::Accepted" is doesn't work.:
I have already said that QDialogButtonBox::accepted is a dialogbuttonbox signal, and is not the same as QDialog::accepted() (signal) or QDialog::accept() (slot), which is why I typed it all in. Unless you know better.
I dont know better, but I just checked the documentation and found this :)
[Edit:QDialogButtonBox
signal mistaken forQDialog
]When a button is clicked in the button box, the clicked() signal is emitted for the actual button is that is pressed. For convenience, if the button has an AcceptRole, RejectRole, or HelpRole, the accepted(), rejected(), or helpRequested() signals are emitted respectively.
( https://doc.qt.io/qt-6/qdialogbuttonbox.html#details)
And AFAIK these Roles are applied automatically if you create a buttonBox in Designer.
[ Edit: Yes, but only the buttonBox ones... :) ]I'm also confused right now :D
BTW: @MyNameIsQt sorry for discussing in your topic :)
If you need theif (dlg->exec() == QDialog::Accepted)
-check only to callclose()
, you can remove it.
Then it's up to you whether you move fromexec()
todlg->open()
.
After that, check if the signals are still not emitted.@Pl45m4
My friend, one more time, I have already written this several times! Please read what I have written, then I don't have to keep typing the same thing! Those areQDialogButtonBox
signals, which IS NOT THE SAME asQDialog
signals. Signals on different objects which happen to have the same name, like,accepted()
. I really don't know how much clearer I can be.QDialogButtonBox::accepted()
signal does NOT raise/emitQDialog::accepted()
signal. That is precisely why the example shows having to attachQDialogButtonBox::accepted()
(signal) ->QDialog::accept()
(slot) ->QDialog::accepted()
(signal).If you don't think so why don't you try code with a
QDialogButtonBox
but without theconnect()
s, then you/we will know? -
@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@JonB just realized that the connect isn't necessarily needed. If these buttons are StandardButtons like QDialogButtonBox::Ok, which already has the AcceptRole and therefore triggers QDialog::Accept on click.
Then please explain why https://doc.qt.io/qt-6/qdialogbuttonbox.html#details has the following explanation plus code:
Alternatively, QDialogButtonBox provides several standard buttons (e.g. OK, Cancel, Save) that you can use. They exist as flags so you can OR them together in the constructor.
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
? TBH I am getting a bit tired of answering the same each time. I have already said that
QDialogButtonBox::accepted
is a dialogbuttonbox signal, and is not the same asQDialog::accepted()
(signal) orQDialog::accept()
(slot), which is why I typed it all in. Unless you know better.@JonB Thanks so much.
This is work!ProgressDlg::ProgressDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ProgressDlg) , m_canceled(false) { ui->setupUi(this); ui->progressBar->setRange(0, 100); ui->progressBar->setValue(0); // ui->buttonBox->setEnabled(false); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); } .... void WebpEditor::on_actionProgress_triggered() { ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->show(); if (dlg->exec() == QDialog::Accepted) { dlg->close(); } }
It's not Problem of QDialogButtonBox; :)
Thanks so much. and @Pl45m4 Thanks too. -
-
@JonB Thanks so much.
This is work!ProgressDlg::ProgressDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ProgressDlg) , m_canceled(false) { ui->setupUi(this); ui->progressBar->setRange(0, 100); ui->progressBar->setValue(0); // ui->buttonBox->setEnabled(false); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); } .... void WebpEditor::on_actionProgress_triggered() { ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->show(); if (dlg->exec() == QDialog::Accepted) { dlg->close(); } }
It's not Problem of QDialogButtonBox; :)
Thanks so much. and @Pl45m4 Thanks too.@MyNameIsQt Wow Sorry
This is it.if (dlg->exec() == QDialog::Accepted) { ... }
not this
if (dlg->exec() == QDialog::Accepted) { dlg->close(); // "close()" is don't need it... }
-
@JonB Thanks so much.
This is work!ProgressDlg::ProgressDlg(QWidget *parent) : QDialog(parent) , ui(new Ui::ProgressDlg) , m_canceled(false) { ui->setupUi(this); ui->progressBar->setRange(0, 100); ui->progressBar->setValue(0); // ui->buttonBox->setEnabled(false); connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); } .... void WebpEditor::on_actionProgress_triggered() { ProgressDlg *dlg = new ProgressDlg(this); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setMessage("Saving .Wpub Files"); dlg->show(); if (dlg->exec() == QDialog::Accepted) { dlg->close(); } }
It's not Problem of QDialogButtonBox; :)
Thanks so much. and @Pl45m4 Thanks too.@MyNameIsQt
I am still worried by your code.-
dlg->show();
prior todlg->exec()
is not necessary, but not harmful. -
The
dlg->close()
afterdlg->exec()
worries me because you previously setdlg->setAttribute(Qt::WA_DeleteOnClose);
. When the user accepts the dialog it is going to get deleted by theexec()
finishing. But you still godlg->close();
ondlg
which might have been deleted by then, and that would cause a problem. It may work because theQt::WA_DeleteOnClose
actually callsdlg->deleteLater()
rather thandelete dlg
, I don't know. If it were me I would remove thedlg->close()
and verify the dialog still closes on return fromexec()
, and leave it at that.
UPDATE
I think you have just posted your latest code above with this removed, good! -
-
@Pl45m4
My friend, one more time, I have already written this several times! Please read what I have written, then I don't have to keep typing the same thing! Those areQDialogButtonBox
signals, which IS NOT THE SAME asQDialog
signals. Signals on different objects which happen to have the same name, like,accepted()
. I really don't know how much clearer I can be.QDialogButtonBox::accepted()
signal does NOT raise/emitQDialog::accepted()
signal. That is precisely why the example shows having to attachQDialogButtonBox::accepted()
(signal) ->QDialog::accept()
(slot) ->QDialog::accepted()
(signal).If you don't think so why don't you try code with a
QDialogButtonBox
but without theconnect()
s, then you/we will know?@JonB said in " QDialog::Accepted" is doesn't work.:
Signals on different objects which happen to have the same name, like, accepted(). I really don't know how much clearer I can be. QDialogButtonBox::accepted() signal does NOT raise/emit QDialog::accepted() signal
@JonB ... whoops :o)
@MyNameIsQt Yes, the new version looks much better :)
-
@JonB said in " QDialog::Accepted" is doesn't work.:
Signals on different objects which happen to have the same name, like, accepted(). I really don't know how much clearer I can be. QDialogButtonBox::accepted() signal does NOT raise/emit QDialog::accepted() signal
@JonB ... whoops :o)
@MyNameIsQt Yes, the new version looks much better :)
-
@Pl45m4 said in " QDialog::Accepted" is doesn't work.:
@JonB ... whoops :o)
Penny dropped :)
It's a bit fast, but is that a video of Burt Reynolds?? If so which movie?