Hi,
my QDialog with a QBUttonBox close automatically of i press "Abort" or "Cancel".
Is there a way to configure the QButtonBox NOT to close the QDialog automatically on "Abort" or "Cancel"?
Thx
Printable View
Hi,
my QDialog with a QBUttonBox close automatically of i press "Abort" or "Cancel".
Is there a way to configure the QButtonBox NOT to close the QDialog automatically on "Abort" or "Cancel"?
Thx
Those buttons are usually connected to the dialog's reject() slot.
This slot is also invoked when the dialog is closed via the window decoration or global shortcut (e.g. ALT+F4).
You can easily intercept this form of closing by overwriting reject() and just returning when you don't want to close, and just calling the base class implementation if you do.
Cheers,
_
QDialog::done(int r) will work too
Validate-Data-in-QDialog
it will accept Accepted() or Rejected() signal before it really executed
Tte problem is, that reject() is a signal and not a slot.
http://doc.qt.io/qt-4.8/qdialogbuttonbox.html#rejected
Ok, my fault, i looked at a wrong class. I have now overloaded QDialog::reject() inside my dialogtest.cpp
I have no idea how to modify the
that it only call done(Rejected) if the button "Abort" was pressed. Hope you couldCode:
void DialogTest::reject()
give me hint, still a Qt and C++ beginner since 3 month :)
Thx
Code:
#include "dialogtest.h" #include "ui_dialogtest.h" ui(new Ui::DialogTest) { ui->setupUi(this); this->setModal( true ); } DialogTest::~DialogTest() { delete ui; } { switch (stdButton) { qDebug() << Q_FUNC_INFO << "OK"; break; qDebug() << Q_FUNC_INFO << "Discard"; break; qDebug() << Q_FUNC_INFO << "Abort"; break; qDebug() << Q_FUNC_INFO << "Cancel"; break; qDebug() << Q_FUNC_INFO << "Apply"; break; default: qDebug() << Q_FUNC_INFO << "button not handled"; break; } } void DialogTest::reject() { qDebug() << Q_FUNC_INFO << "QDialog::reject()"; done(Rejected); } void DialogTest::accept() { qDebug() << Q_FUNC_INFO << "QDialog::reject()"; done(Accepted); }
I am not sure what you want to do now.
You asked for not closing the dialog on abort or cancel.
The way to do that is to overwrite reject() and return until you want to close.
If you want to close, you call the base implementation, i.e. QDialog::reject().
Now you write that you want to close when Abort is clicked?
That seems to be the opposite of what you wrote before.
Maybe you should clarify what you want to happen exactly when.
Cheers,
_
That was obviously becauseQuote:
that it only call done(Rejected) if the button "Abort" was pressed.
AND your reject() slot isQuote:
Those buttons are usually connected to the dialog's reject() slot.
Code:
void DialogTest::reject() { qDebug() << Q_FUNC_INFO << "QDialog::reject()"; done(Rejected); }
So if you want to not closing the dialog, you should make handler for "abort" button inside void reject() slot
something like -- IF "abort" pressed then return, ELSE done(rejected) --
This is odd behavior for a modal dialog. You must only choose 'accept' ?
You could make the dialog modeless (use show/hide instead of exec) if you want to have it always visible without interfering with other parts of the user interface(?). You could also disable the 'cancel' button (?).
To prevent 'abort' or 'cancel' from closing the dialog you can subclass the dialog closeEvant(QCloseEvent*) virtual method. If something tries to close the dialog (including clicking on the little 'x' in the title bar) you can block it here.