C++ QDialog::accepted()插槽不工作

C++ QDialog::accepted()插槽不工作,c++,qt,signals-slots,qdialog,C++,Qt,Signals Slots,Qdialog,我正在尝试为QDialog实现一个信号槽系统。在谷歌搜索之后,我发现了堆栈溢出。这个答案看起来很有希望,所以我试着用它。我没有收到错误,但插槽不工作。下面是我的代码: newactivity.cpp // in the QDialog constructor QObject::connect(this->ui.createBtn, SIGNAL(clicked()), this, SLOT(accept())); QObject::connect(this->ui.cancelBtn

我正在尝试为
QDialog
实现一个信号槽系统。在谷歌搜索之后,我发现了堆栈溢出。这个答案看起来很有希望,所以我试着用它。我没有收到错误,但插槽不工作。下面是我的代码:

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
}
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();
}
时间表.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
}
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();
}
这是我的对话:


当我按下“新建活动”对话框上的“创建”按钮时,不会发生任何事情。我哪里错了?

我想您应该将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(); 
}

这能解决您的问题吗?

我想您可以将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(); 
}

这能解决你的问题吗?

是的。它解决了这个问题。这是我想到的最后一件事……:)很高兴提供帮助,实际上这是因为由
exec()
启动的eventloop不再移动到下一行代码。当我找到相应的解释时,我会在这里留下一个链接。若你们的问题解决了,别忘了将答案标记为已接受:是的。它解决了这个问题。这是我想到的最后一件事……:)很高兴提供帮助,实际上这是因为由
exec()
启动的eventloop不再移动到下一行代码。当我找到相应的解释时,我会在这里留下一个链接。若你们的问题解决了,别忘了将答案标记为已接受:P