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. Disable close button in MainWindow
Forum Updated to NodeBB v4.3 + New Features

Disable close button in MainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 5 Posters 15.8k Views 2 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
    MichiS97
    wrote on last edited by
    #1

    Hi,
    I'm currently in the process of writing my first Qt Application with a GUI that consists of more than one window, so please bear with me if this is a stupid question.

    Basically, I have a MainWindow class with a couple of buttons where each button opens another window. My problem is that the main window can still be interacted with by the user even if one of the subwindows are open. I was able to disable all of the buttons and the menu strip once one of the windows are opened and re-enable them after they're closed but what I just can't manage is to deactivtate the close button (talking about the standard windows "X"-Button in the top right corner). Is there any way to accomplish this? I don't want to hide the button or anything, I just want to make sure that the main window can't be closed if any of the child windows are open.

    Thanks in advance,

    Michael

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

      Hi
      Do you want the user to be able to interact with MainWindow while
      subwindows are open?
      You can open the subwindows as modal and user cannot click on main .
      subWin->setWindowModality(Qt::WindowModal);

      Anyway, u can override MainWindow::closeEvent (QCloseEvent *event)
      and disallow to close if u wish
      http://stackoverflow.com/questions/17480984/qt-how-do-i-handle-the-event-of-the-user-pressing-the-x-close-button

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Why not just show other windows as modal (http://doc.qt.io/qt-5/qwindow.html#modality-prop)?

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

        1 Reply Last reply
        1
        • M Offline
          M Offline
          MichiS97
          wrote on last edited by
          #4

          I didn't know anything about window modality, thank you very much!

          RatzzR 1 Reply Last reply
          0
          • M MichiS97

            I didn't know anything about window modality, thank you very much!

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by Ratzz
            #5

            @MichiS97
            Disabling Close button is a bad idea.Best way is setting WindowModality as already suggested. But you can still do it via flags.

                Qt::WindowFlags flags = windowFlags();
                Qt::WindowFlags closeFlag = Qt::WindowCloseButtonHint;
                flags = flags & (~closeFlag);
                setWindowFlags(flags);

            --Alles ist gut.

            kshegunovK 1 Reply Last reply
            1
            • RatzzR Ratzz

              @MichiS97
              Disabling Close button is a bad idea.Best way is setting WindowModality as already suggested. But you can still do it via flags.

                  Qt::WindowFlags flags = windowFlags();
                  Qt::WindowFlags closeFlag = Qt::WindowCloseButtonHint;
                  flags = flags & (~closeFlag);
                  setWindowFlags(flags);
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Ratzz @MichiS97

              Disabling Close button is a bad idea.

              It's a terrible idea, I agree.

              But you can still do it via flags.

              Not exactly true. You can pass the flags to the window manager, but it can choose to disrespect them! So, on some WMs it will work, on some it might not. Additionally (I hope my memory serves me here) some flags are to be provided for the constructor and can't be set/cleared after QWidget::create has run.

              Read and abide by the 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