Important: Please read the Qt Code of Conduct -

Disable close button in MainWindow

  • 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,


  • Lifetime Qt Champion

    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 .

    Anyway, u can override MainWindow::closeEvent (QCloseEvent *event)
    and disallow to close if u wish

  • Lifetime Qt Champion

    Why not just show other windows as modal (

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

  • @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);

  • Moderators

    @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.

Log in to reply