3

I have a dialog AlarmSetup derived from QDialog with following button arrangement:

// button box
buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel |ButtonBox::Help, Qt::Horizontal, this);
buttonBox->button(QDialogButtonBox::Ok)->setText("übernehmen");
buttonBox->button(QDialogButtonBox::Cancel)->setText("abbrechen");
buttonBox->button(QDialogButtonBox::Help)->setText("Hilfe");
connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotOk()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(slotCancel()));
connect(buttonBox, SIGNAL(helpRequested()), this, SLOT(slotHelp()));

From a second dialog AlarmWindow, I have a slot AlarmWindow::slotOpen() in which I am creating a new instance of AlarmSetup and evaluating the return code of AlarmSetup::exec():

void AlarmWindow::slotOpen()   // we create a new instance of AlarmSetup
{
if ( DBG_ALARM ) qDebug() << "AlarmWindow::slotOpen() triggered";

int alarmId = mAlarm.getAlarmIdFromAlarmMap( objectName() );
AlarmData alarmData = mAlarm.mAlarmMap.value( alarmId );
//qDebug() << "alarmData:" << alarmData << "| alarmId:" << alarmId;

AlarmSetup* alarmSetup = new  AlarmSetup( mAlarm, alarmData, alarmId );
int res = alarmSetup->exec();
qDebug() << "AlarmWindow -> AlarmSetup() returned:" << (res==QDialog::Accepted? "QDialog::Accepted":"QDialog::Rejected");
...
}

res is always QDialog::Rejected, independly which button I clicked in AlarmSetup! The corresponding buttons are standard button QDialogButtonBox::Ok and QDialogButtonBox::Cancel respectively, the corresponding signals SIGNAL(accepted()) and SIGNAL(rejected()) respectively, so I do not understand why the return value is wrong!
Note that the dialog AlarmSetup is working as expected.

Any solution to get return value res working?

Thank you for your time.

here is the code of slotOK()

void AlarmSetup::slotOk()
{
if (DBG_ALARM) qDebug() << "AlarmSetup::slotOk() triggered";

QTime time = timeBox->time();
time.addSecs(60);                                                   // next full minute
time.setHMS( time.hour(), time.minute(), 0 );
AlarmData alarmData( alarmActiveBox->isChecked()
                   , QDateTime( calendar->selectedDate(), time )
                   , titleBox->text()
                   , textBox->document()->toPlainText()
                   , alarmSound->isChecked()
                   , alarmSoundBox->text()
                   , alarmRepeatActive->isChecked()
                   , numBox->text().toInt()
                   , unitBox->currentText()
                   , mFileName );

//qDebug() << "data from Setup:" << alarmData;
emit signalSetAlarm( alarmData, mAlarmId );

close();
}
Alain Weiler
  • 139
  • 1
  • 11

1 Answers1

3

Call accept(); or reject(); instead of close(). Rejected is just the default value (as by pressing ESC key).

Change your slots to return the desired value.

Update:

This works for me:

Mainwindow (removed irrelevant methods):

void MainWindow::openDialog()
{
    Dialog* dialog = new Dialog();
    dialog->setModal(true);
    int result = dialog->exec();
    qDebug()<<"Result:"<<result;
}

Dialog (removed irrelevant methods):

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

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
    buttonBox->button(QDialogButtonBox::Ok)->setText("übernehmen");
    buttonBox->button(QDialogButtonBox::Cancel)->setText("abbrechen");
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotOk()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(slotCancel()));
}

void Dialog::slotOk()
{
    accept();
    //close();
}

void Dialog::slotCancel()
{
    reject();
}

With close() instead of accept() I receive 0 for result too. This may be due to being in the button box and not gaining the correct role, not sure though.

Did you override QDialog::exec() or QDialog::accept()?

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • I have made the changes as proposed, the returning code is still always 0. In my view, the kind of action should be in accordance to the button type and the signal it is emiting and not in the name of the slot called. – Alain Weiler Mar 02 '17 at 23:53
  • The documentation is also reflecting this point of view, see enum QDialogButtonBox::StandardButton – Alain Weiler Mar 03 '17 at 00:03
  • now it is working as expected. I called my slot accept() and reject() instead of adding those functions to my slots. Mea culpa. Vielen Dank Sebastian! – Alain Weiler Mar 03 '17 at 10:54