Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Implementing a cancel button
Forum Updated to NodeBB v4.3 + New Features

[Solved]Implementing a cancel button

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 6.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrplainswalker
    wrote on 17 Aug 2011, 20:58 last edited by
    #1

    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);
    newRecipeWindow->setAttribute(Qt::WA_DeleteOnClose);
    newRecipeWindow->show();
    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 17 Aug 2011, 22:11 last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrplainswalker
        wrote on 18 Aug 2011, 14:15 last edited by
        #3

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 18 Aug 2011, 21:27 last edited by
          #4

          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 :)

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • V Offline
            V Offline
            valdomiromorais
            wrote on 8 Jun 2012, 06:28 last edited by
            #5

            This is my solution:

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved