【问题标题】:QDialog::accepted() slot not workingQDialog::accepted() 插槽不工作
【发布时间】:2014-06-14 10:08:15
【问题描述】:

我正在尝试为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();
}

这是我的对话:

当我按下New activity 对话框上的Create 按钮时,什么也没有发生。我哪里错了?

【问题讨论】:

    标签: c++ qt signals-slots qdialog


    【解决方案1】:

    我想您将 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() 启动的事件循环,我想它不再移动到下一行代码。当我找到相应的解释时,我会在这里留下一个链接。如果您的问题得到解决,请不要忘记将答案标记为已接受:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多