Leaves One

Alan Richard's Blog

Hide Close Button for PyQt MDI Subwindow

Problem

PyQt6 (also applys for PyQt5), MDI subwindow’s close button is not hidden.

Close button not hiding

Code snippet:

# Create QMdiArea
w.mdi = QMdiArea(w)
w.mdi.setGeometry(w.geometry())

# Create subwindow
sub = QMdiSubWindow()
sub.setWidget(QTextEdit("Alan Richard"))
sub.setWindowTitle("SubWindow")

w.mdi.addSubWindow(sub) # Add to MDI area

# !!! Try to hide close button. It works for main window, but not for MDI subwindow.
sub.setWindowFlags(sub.windowFlags() | Qt.WindowType.CustomizeWindowHint)
sub.setWindowFlags(sub.windowFlags() & ~Qt.WindowType.WindowCloseButtonHint)

Solution

Do not use QMdiSubWindow in this case. Instead, add a WindowMinMaxButtonsHint when adding widget to MDI area.

Code snippet:

# ...
# Create subwindow
sub = QTextEdit("Alan Richard")
w.mdi.addSubWindow(sub, Qt.WindowType.WindowMinMaxButtonsHint) # <--
sub.setWindowTitle("SubWindow")

w.mdi.addSubWindow(sub) # Add to MDI area

# Removed `setWindowFlag` lines

Solved

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
If you checked “Remember me”, your email address and name will be stored in your browser for your convenience.