PDA

View Full Version : QButtonBox, QDialog should not close on "Abort" or "Cancel"



HappyCoder
8th September 2015, 16:03
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

anda_skoa
8th September 2015, 16:15
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,
_

Frdz
8th September 2015, 16:30
QDialog::done(int r) will work too
Validate-Data-in-QDialog (http://www.qtcentre.org/threads/8048-Validate-Data-in-QDialog)
it will accept Accepted() or Rejected() signal before it really executed

HappyCoder
9th September 2015, 12:39
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,
_

Tte problem is, that reject() is a signal and not a slot.
http://doc.qt.io/qt-4.8/qdialogbuttonbox.html#rejected

anda_skoa
9th September 2015, 12:45
Tte problem is, that reject() is a signal and not a slot.

No, it isn't. It is a slot.



http://doc.qt.io/qt-4.8/qdialogbuttonbox.html#rejected
Yes, that is a signal. With a different name. On a different class.

Cheers,
_

HappyCoder
9th September 2015, 17:58
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


void DialogTest::reject()

that it only call done(Rejected) if the button "Abort" was pressed. Hope you could
give me hint, still a Qt and C++ beginner since 3 month :)
Thx




#include "dialogtest.h"
#include "ui_dialogtest.h"

DialogTest::DialogTest(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogTest)
{
ui->setupUi(this);
this->setModal( true );

connect( ui->buttonBox, SIGNAL(clicked(QAbstractButton*)),
this, SLOT(buttonClicked(QAbstractButton*)));
}

DialogTest::~DialogTest()
{
delete ui;
}

void DialogTest::buttonClicked( QAbstractButton *button )
{
QDialogButtonBox::StandardButton stdButton = ui->buttonBox->standardButton(button);
switch (stdButton)
{
case QDialogButtonBox::Ok:
qDebug() << Q_FUNC_INFO << "OK";
break;
case QDialogButtonBox::Discard:
qDebug() << Q_FUNC_INFO << "Discard";
break;
case QDialogButtonBox::Abort:
qDebug() << Q_FUNC_INFO << "Abort";
break;
case QDialogButtonBox::Cancel:
qDebug() << Q_FUNC_INFO << "Cancel";
break;
case QDialogButtonBox::Apply:
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);
}

anda_skoa
9th September 2015, 23:05
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,
_

Frdz
10th September 2015, 09:42
that it only call done(Rejected) if the button "Abort" was pressed.
That was obviously because

Those buttons are usually connected to the dialog's reject() slot.
AND your reject() slot is


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) --

Rondog
10th September 2015, 14:10
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

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.