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] Closing dialogs doesn't work when mainwindow is closed
Forum Updated to NodeBB v4.3 + New Features

[Solved] Closing dialogs doesn't work when mainwindow is closed

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 3.8k 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.
  • E Offline
    E Offline
    ebruurs
    wrote on last edited by
    #1

    Hi,

    I have 3 classes in use:

    • MainWindow: This is my main application and is being showed when my program starts. It got a menubar with some functionality on it.
    • Controller: When a functionality is clicked a controller will be opened. When this controller is being constructed it call Dialog to show a dialog to the user.
    • Dialog

    The problem is that when I click on functionality A, the dialog of A will be shown. However if I close my window now by pressing 'x' my dialog won't close.

    Here are some codesnapshots

    controller.h
    @
    class Controller : public QObject {
    Q_OBJECT

    public:
    Controller();
    virtual ~Controller();
    @
    I have chosen for QObject since the controller contains some slots and thougt this is the most general type that fit the idea of the controller.

    controller.cpp
    @
    Controller::Controller() {
    Dialog d = new Dialog(this);
    d->show();
    }
    @

    mainwindow.cpp
    @
    Controller *controller = new Controller();
    @

    Don't think the code of dialog is interesting since it only places some widgets on the dialog and calls for the controller when a signal is clicked.

    Thank you in advance for helping me out.

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Since the controller object is the parent for the Dialog instance i assume that you forgot to delete it in the mainwindow destructor (an alternative is to use the parent-child relationship to give the controller a parent - the mainwindow instance)

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mr_gui
        wrote on last edited by
        #3

        I think your Controller object has to be in any relationship to your mainwindow object. you should try to set mainwindow as parent of your controller object.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          ebruurs
          wrote on last edited by
          #4

          I tried to add the parent-child relationship to the controller and the mainWindow. but it didnt solved the problem. Altho i am not shure if i implemented the parent-child relationship well.

          controller.h
          @
          class Controller : public QObject {
          Q_OBJECT

          public:
          Controller(QWidget*);
          virtual ~Controller();
          @
          controller.cpp
          @
          Controller::Controller(QWidget *p) :QObject(p) {
          Dialog d = new Dialog(this);
          d->show();
          }
          @
          mainwindow.cpp
          @
          Controller *controller = new Controller(this);
          @

          1 Reply Last reply
          0
          • ZlatomirZ Offline
            ZlatomirZ Offline
            Zlatomir
            wrote on last edited by
            #5

            That should work, the Dialog class takes the controller parent? QDialog expects a QWidget* as a parent, make sure your error isn't there

            Check with the debugger to see where the "chain" fails.

            Or another method will be to set the Controller's parent as a parent to the Dialog too:
            @
            Controller::Controller(QWidget *p) :QObject(p) {
            Dialog d = new Dialog(p); //pass p as a parent for the dialog
            //since p should be the pointer to mainwindow instance
            d->show();
            }
            @

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              This relationship will not help, as Controller is no widget. So the dialog will be a top level dialog.
              Event the controller object will be destroyed when the main window object is destroyed, not when the main window is closed. The mainwindow object will be destroyed, when the event loop is exited and main exits :-)

              If MainWindow would be the parent of the dialog, it should work

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ebruurs
                wrote on last edited by
                #7

                Thank you Gerolf, this worked. I give now my Dialog class the mainwindow as a parent.

                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