Important: Please read the Qt Code of Conduct -

[Solved]Implementing a cancel button

  • I'm trying to implement a cancel button in a widget inside of a QMdiArea. However, whenever I call close on the widget, the widget closes, but the QMdiSubWindow that I used to add it to the QMdiArea still remains open. I worked around this using a close signal, but it seems incredibly clunky. Is there a better way to handle this? Relevant code is below.

    In my main window, I have a function that creates a new subwindow:

    @void MainWindow::addNewRecipe()
    NewRecipeWindow newRecipe = new NewRecipeWindow((QWidget)this, m_RecipeBook);
    QMdiSubWindow *newRecipeWindow = m_CentralArea->addSubWindow(newRecipe);
    connect(newRecipe, SIGNAL(closeThisWindow()), newRecipeWindow, SLOT(close()));

    Note that NewRecipeWindow inherits QWidget. In my NewRecipeWindow class:

    @void NewRecipeWindow::cancel()
    emit closeThisWindow();

    This function is linked to the cancel button. It seems like I shouldn't need an extra layer of functions, but I can't figure out a way around it. I can't use the same function as both a signal and a slot. I could get a pointer directly to the sub window's close button in the main window, but I don't like that solution.

  • I see no other solution. QWidget is not aware of the QMdiSubwindow, it is placed in, and that's ok - it could be part of a QTabWidget that has no such concept.

    Of course you can connect a signal to another signal:

    connect(ui->btnClose, SIGNAL(clicked()), this, SIGNAL(closeThisWindow()));

    Which just emits the closeThisWindow signal. This will save you the slot that just emits the signal.

  • Ok, that's actually exactly what I was looking for. I didn't realize you could connect two signals. Thanks for the help!

  • You're welcome. It's quite worth to just read the complet API docs of [[Doc:QObject]] and [[Doc:QWidget]] (and some others too), there are some nice gems inbetween that ease your live as a developer :)

  • This is my solution:

    @QMdiSubWindow *subWin = mdiArea->addSubWindow(dialogo);
    connect( dialogo->getBtnCancel(),SIGNAL(clicked()),subWin,SLOT(close()));

Log in to reply