1

I am trying to implement a signal-slot system for a QDialog. After a Google search, I've got this question on Stack Overflow. That answer looks promising, so I tried to use it. I get no error, but the slot doesn't work. Below is my code:

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

Here is my dialog:

enter image description here

Nothing happens when I press the Create button on New activity dialog. Where am I wrong?

Community
  • 1
  • 1
Victor
  • 13,914
  • 19
  • 78
  • 147

1 Answers1

4

I suppose you reorder the code in schedule.cpp as:

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

Does this solve your problem?

  • Yeah. It solves the problem. This was the last thing that would have came to my mind... :)) – Victor Jun 14 '14 at 10:16
  • Glad to help, actually this happens because of the eventloop started by `exec()` which no more moves to next lines of code I suppose.I will leave a link down here when I will find corresponding explanation. Don't forget to mark answer as accepted if your problem got solved :P –  Jun 14 '14 at 10:19