QDialog :: accepted()插槽不起作用

时间:2014-06-14 10:08:16

标签: c++ qt signals-slots qdialog

我正在尝试为QDialog实现信号槽系统。在Google搜索之后,我在Stack Overflow上获得了this question。这个答案看起来很有希望,所以我试着用它。我没有错误,但插槽没有工作。以下是我的代码:

newactivity.cpp

// in the QDialog constructor
QObject::connect(this->ui.createBtn, SIGNAL(clicked()), this, SLOT(accept()));
QObject::connect(this->ui.cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));

void newActivity::accept() {
    QDialog::accept(); // to close the dialog and return 1
}
void newActivity::reject() {
    QDialog::reject(); // to close the dialog and return 0
}

schedule.cpp

void Schedule::on_actionNew_Activity_triggered() {
    newActivity *newActivityWnd = new newActivity(this, Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
    newActivityWnd->exec(); 
    QObject::connect(newActivityWnd, SIGNAL(accepted()), this, SLOT(on_activityCreated()));
}

void Schedule::on_activityCreated() {
    this->ui.timeLine->hide();
}

这是我的对话框:

enter image description here

当我按下New activity对话框上的创建按钮时,没有任何反应。我哪里错了?

1 个答案:

答案 0 :(得分:4)

我想您将schedule.cpp中的代码重新排序为:

void Schedule::on_actionNew_Activity_triggered() {
    newActivity *newActivityWnd = new newActivity(this, Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
    QObject::connect(newActivityWnd, SIGNAL(accepted()), this, SLOT(on_activityCreated()));
    newActivityWnd->exec(); 
}

这会解决您的问题吗?