It is not necessary for you to emit the QDialog::accepted() signal. This signal is emitted by the QDialog base class when you call the QDialog::accept() method.
You show three different connect() calls. The first one (line 4) won't compile because "ui->pushButton_4" is a private member of the AddRecipe class (unless your MainWindow class also happens to have a ui->pushButton_4 member, in which case you are connecting the wrong button).
The second (line 5) looks correct, as does the one in line 9. If you added a signal-slot connection in Qt Designer when designing your dialog, then you probably have two slots being executed when the button is clicked: the one you manually connect to in line 9, and the one that is generated for you by Qt Designer that connects the button's clicked() signal to on_pushButton4_clicked().
What you don't show us is the code where you actually show the AddRecipe dialog. My hunch is that your problem lies there.
Edit: Ah, I noticed that you attached the zip file with your code. My hunch is correct.
void MainWindow::on_actionAdd_New_triggered()
{
AddRecipe mDialogAddRecipe;
mDialogAddRecipe.setModal(true);
mDialogAddRecipe.exec();
}
void MainWindow::on_actionAdd_New_triggered()
{
AddRecipe mDialogAddRecipe;
mDialogAddRecipe.setModal(true);
mDialogAddRecipe.exec();
}
To copy to clipboard, switch view to plain text mode
You do understand that the AddRecipe instance you create -on the stack- here is not the same instance that you created in the MainWindow constructor, right? This instance has no connections to anything. And you also understand that the instance you create -on the heap- in the MainWindow constructor becomes basically an inaccessible dangling pointer once the constructor exits because you haven't saved the pointer anywhere, right?
The easiest solution is to get rid of the code in the constructor that deals with this dialog and move it into the slot:
void MainWindow::on_actionAdd_New_triggered()
{
AddRecipe addRecipes ( this );
connect( &addrecipes,SIGNAL(accepted()),this,SLOT(show_table()));
addRecipes.exec();
}
void MainWindow::on_actionAdd_New_triggered()
{
AddRecipe addRecipes ( this );
connect( &addrecipes,SIGNAL(accepted()),this,SLOT(show_table()));
addRecipes.exec();
}
To copy to clipboard, switch view to plain text mode
Calling setModal() is redundant. By calling exec(), you -are- executing it as a modal dialog. Delete these lines from the MainWindow constructor:
AddRecipe * addrecipes = new AddRecipe;
connect(ui->pushButton_4,SIGNAL(released()),this,SLOT(show_table()));
connect(addrecipes,SIGNAL(accepted()),this,SLOT(show_table()));
AddRecipe * addrecipes = new AddRecipe;
connect(ui->pushButton_4,SIGNAL(released()),this,SLOT(show_table()));
connect(addrecipes,SIGNAL(accepted()),this,SLOT(show_table()));
To copy to clipboard, switch view to plain text mode
I haven't looked at the rest of your code, but unless your AddRecipe dialog somehow magically knows about your recipe database, anything that happens in the dialog disappears as soon as it closes because there aren't any other signals or slots connected to MainWindow or any data structures being retrieved from the dialog that tell MainWindow what has happened there. MainWindow's show_table() -can- access the dialog, but only temporarily, using the sender() method inside the show_table() slot. But once that slot exits, the dialog is gone.
Bookmarks