QDialog :: exec()的返回值始终是QDialog :: Rejected

时间:2017-03-02 13:56:19

标签: qt

我有一个从QDialog派生的对话框AlarmSetup,按钮排列如下:

// 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()));

从第二个对话框AlarmWindow,我有一个插槽AlarmWindow :: slotOpen(),我在其中创建一个新的AlarmSetup实例并评估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始终是QDialog :: Rejected,我在AlarmSetup中单击了哪个按钮! 相应的按钮分别是标准按钮QDialogBu​​ttonBox :: Ok和QDialogBu​​ttonBox :: Cancel,分别对应信号SIGNAL(accepted())和SIGNAL(rejected()),所以我不明白为什么返回值是错误的!
请注意,对话框AlarmSetup正在按预期工作。

任何获得返回值的解决方案都能正常工作吗?

感谢您的时间。

这是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();
}

1 个答案:

答案 0 :(得分:0)

致电accept();reject();而非close()。拒绝只是默认值(通过按ESC键)。

更改广告位以返回所需的值。

<强>更新

这对我有用:

Mainwindow(删除不相关的方法):

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

Dialog(删除不相关的方法):

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();
}

使用close()代替accept()我也会收到0作为结果。这可能是由于在按钮框中而没有获得正确的角色,但不确定。

您是否覆盖了QDialog::exec()QDialog::accept()