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. How to use "try destroy window" event?
Forum Updated to NodeBB v4.3 + New Features

How to use "try destroy window" event?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 684 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.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by
    #1

    I have descendent by QMainWindow, QTabWidget with editors CodeEditor : public QPlainTextEdit.
    Editor can be closed by many way : click X on tab, Ctrl+F4, menu File->Close and even exit program witgh Alt+F4.
    I want not enable closing not saved editors until user not choose (Yes,No, Cancel).
    How do it?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You are in luck. That use case is the example from the docs :)
      https://doc.qt.io/qt-5/qwidget.html#closeEvent

      So you override this function and its called when user tried to close app and then you can
      reject it or allow it as you wish.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        AndrzejB
        wrote on last edited by
        #3

        But how to do with closeEvent not in MainWindow but CodeEditor which is tabWidget widget?

        void CodeEditor::closeEvent(QCloseEvent *event)
        {
            if (document()->isModified()){
                event->ignore();
            } else {
                event->accept();
            }
        }
        

        when I debug, it call ignore when modified but anyway widget is closed

        mrjjM 1 Reply Last reply
        0
        • A AndrzejB

          But how to do with closeEvent not in MainWindow but CodeEditor which is tabWidget widget?

          void CodeEditor::closeEvent(QCloseEvent *event)
          {
              if (document()->isModified()){
                  event->ignore();
              } else {
                  event->accept();
              }
          }
          

          when I debug, it call ignore when modified but anyway widget is closed

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AndrzejB

          But is CodeEditor inside other widget ?
          Else im not really sure why ignore wont work.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndrzejB
            wrote on last edited by
            #5

            Editor is inside tab

            newEditor = new CodeEditor(repository, filePath, this);
            newEditor->openFile(filePath);
            ...
            tabWidget->addTab(newEditor, title);
            tabWidget->setCurrentWidget(newEditor);
            
            mrjjM 1 Reply Last reply
            0
            • A AndrzejB

              Editor is inside tab

              newEditor = new CodeEditor(repository, filePath, this);
              newEditor->openFile(filePath);
              ...
              tabWidget->addTab(newEditor, title);
              tabWidget->setCurrentWidget(newEditor);
              
              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @AndrzejB
              Ok :)
              You need to handle the closeEvent at the top level widget as the childs cant cancel the parent closing (AFAIK)
              So who ever holds the tabWidget, would be a good canidate to control it.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AndrzejB
                wrote on last edited by
                #7

                I have method

                void TabWindow::closeTab(int index)
                

                where "TabWindow" is my main window. I simply change this method. This method cover closing by tab,by menu and by shortcuts. Only closing whole app is not covered by this method, but closing app event you give me above, thanks!

                mrjjM 1 Reply Last reply
                0
                • A AndrzejB

                  I have method

                  void TabWindow::closeTab(int index)
                  

                  where "TabWindow" is my main window. I simply change this method. This method cover closing by tab,by menu and by shortcuts. Only closing whole app is not covered by this method, but closing app event you give me above, thanks!

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @AndrzejB
                  Ah super. :)
                  Don't forget to mark as solved if you are satisfied.

                  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