Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Deactivating close button in MDI child window



  • Hello,

    I've been trying to deactivate the close button in a MDI child window to no avail.

    I've tried this in the constructor of the MDI QDialog:

    self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)
    

    or this:

    self.setWindowFlags(QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowMinMaxButtonsHint)
    

    But it didn't change anything. Any hint?

    Thanks,
    Mark



  • @Mark531 said in Deactivating close button in MDI child window:

    constructor of the MDI QDialog

    So what exactly is your self?

    A common mistake with MDI sub-windows is failing to understand that the widget you put there is not the one whose flags you want to alter, or call close() on, etc. For a QMdiSubWindow you must understand that is the window you need to operate on, not whatever widget you put into it. Given the widget, you can if necessary access its parent QMdiSubWindow via widget->parentWidget(). Or just something like

    QMdiSubWindow *subWin = mdiArea->addSubWindow(myWidget);
    subWin->setWindowFlags(subWin->windowFlags() & ~QtCore.Qt.WindowCloseButtonHint);
    


  • @JonB said in Deactivating close button in MDI child window:

    subWin->setWindowFlags(subWin->windowFlags() & ~QtCore.Qt.WindowCloseButtonHint);

    You were right, JonB, I first tried to set the flags in the QDialog constructor. But it seems strange to modify the QMdiSubWindow instead, since the window you call the show method on is the QDialog.
    Anyway, for some reason, I had to reset all flags to make it work:

    my_dialog = MyDialog()
    win = MAIN_WINDOW.mdiArea.addSubWindow(my_dialog)
    # DID NOT WORK
    #win.setWindowFlags(win.windowFlags() & ~QtCore.Qt.WindowCloseButtonHint)
    
    # WORKS
    win.setWindowFlags(QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowMinMaxButtonsHint)
    my_dialog.show()
    

Log in to reply