Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Quit the application when QWidget is closed
Forum Updated to NodeBB v4.3 + New Features

Quit the application when QWidget is closed

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 1.4k 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.
  • K Offline
    K Offline
    Knj.Tkm
    wrote on last edited by
    #1

    Hi.
    I want to quit the application when QWidget is closed.
    But there is no signal when QWidget close.

    Is there any way to know that QWidget is closed?

    jsulmJ 1 Reply Last reply
    0
    • K Knj.Tkm

      @jsulm
      QCloseEvent don't work.
      How do I use it?

      void MainWin::closeEv(QCloseEvent *event)
      {
          qDebug() << "HERE";
          event->accept();
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #6

      @Knj-Tkm said in Quit the application when QWidget is closed:

      How do I use it?

      You need to override closeEvent in your class.

      class MainWin: public QMainWindow
      {
      private:
          void closeEvent(QCloseEvent *event) override;
      }
      
      void MainWin::closeEvent(QCloseEvent *event)
      {
          qDebug() << "Closing";
          event->accept();
      }
      

      Is MainWin a QMainWindow? I'm wondering why you need to close your app manually when main window is closed, this should happen automatically.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      1
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by jeremy_k
        #2

        QWidget::showEvent() can be overridden to detect when a widget is hidden.

        QGuiApplication::setQuitOnLastWindowClosed() is likely what you're looking for.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        K 1 Reply Last reply
        1
        • K Knj.Tkm

          Hi.
          I want to quit the application when QWidget is closed.
          But there is no signal when QWidget close.

          Is there any way to know that QWidget is closed?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @Knj-Tkm said in Quit the application when QWidget is closed:

          But there is no signal when QWidget close.

          But there is https://doc.qt.io/qt-5/qwidget.html#closeEvent

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          1
          • jeremy_kJ jeremy_k

            QWidget::showEvent() can be overridden to detect when a widget is hidden.

            QGuiApplication::setQuitOnLastWindowClosed() is likely what you're looking for.

            K Offline
            K Offline
            Knj.Tkm
            wrote on last edited by
            #4

            @jeremy_k
            How to use?
            I try to this code, but no work.

            void MainWin::showEv(QShowEvent *ev)
            {
                QWidget::showEvent(ev);
                qDebug() << "Close";
            }
            
            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Knj-Tkm said in Quit the application when QWidget is closed:

              But there is no signal when QWidget close.

              But there is https://doc.qt.io/qt-5/qwidget.html#closeEvent

              K Offline
              K Offline
              Knj.Tkm
              wrote on last edited by
              #5

              @jsulm
              QCloseEvent don't work.
              How do I use it?

              void MainWin::closeEv(QCloseEvent *event)
              {
                  qDebug() << "HERE";
                  event->accept();
              }
              
              jsulmJ 1 Reply Last reply
              0
              • K Knj.Tkm

                @jsulm
                QCloseEvent don't work.
                How do I use it?

                void MainWin::closeEv(QCloseEvent *event)
                {
                    qDebug() << "HERE";
                    event->accept();
                }
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by jsulm
                #6

                @Knj-Tkm said in Quit the application when QWidget is closed:

                How do I use it?

                You need to override closeEvent in your class.

                class MainWin: public QMainWindow
                {
                private:
                    void closeEvent(QCloseEvent *event) override;
                }
                
                void MainWin::closeEvent(QCloseEvent *event)
                {
                    qDebug() << "Closing";
                    event->accept();
                }
                

                Is MainWin a QMainWindow? I'm wondering why you need to close your app manually when main window is closed, this should happen automatically.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                K 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Knj-Tkm said in Quit the application when QWidget is closed:

                  How do I use it?

                  You need to override closeEvent in your class.

                  class MainWin: public QMainWindow
                  {
                  private:
                      void closeEvent(QCloseEvent *event) override;
                  }
                  
                  void MainWin::closeEvent(QCloseEvent *event)
                  {
                      qDebug() << "Closing";
                      event->accept();
                  }
                  

                  Is MainWin a QMainWindow? I'm wondering why you need to close your app manually when main window is closed, this should happen automatically.

                  K Offline
                  K Offline
                  Knj.Tkm
                  wrote on last edited by Knj.Tkm
                  #7

                  @jsulm
                  It get error
                  [ error: only virtual member functions can be marked 'override']

                  Sorry, It work.
                  I got the code a little wrong.

                  I use QWidget.
                  I'm creating an application window with QWidget.
                  I just want the application to quit when I close QWidget.

                  jsulmJ 1 Reply Last reply
                  0
                  • K Knj.Tkm

                    @jsulm
                    It get error
                    [ error: only virtual member functions can be marked 'override']

                    Sorry, It work.
                    I got the code a little wrong.

                    I use QWidget.
                    I'm creating an application window with QWidget.
                    I just want the application to quit when I close QWidget.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Knj-Tkm The you should rather do what @jeremy_k suggested: set QGuiApplication::setQuitOnLastWindowClosed()

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1

                    • Login

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