Of course! The term setWindowFlags is a fundamental concept in GUI development with the PyQt and PySide (Qt for Python) frameworks. It's used to control the appearance and behavior of a top-level window.

Let's break it down.
What is setWindowFlags?
In Qt, every top-level window (like QMainWindow, QWidget, QDialog) has a set of "window flags." These flags are special attributes that define the window's properties, such as:
- Is it a dialog box or a main window?
- Should it have a title bar and minimize/maximize buttons?
- Should it stay on top of other windows?
- Should it be frameless (no border)?
- Should it be a splash screen?
The setWindowFlags() method allows you to programmatically set these flags for a window.
How to Use setWindowFlags
The method takes a combination of flags as an argument. These flags are constants defined in the QtCore.Qt namespace.

Basic Syntax
# Assuming you have a window object, e.g., a QMainWindow or QWidget your_window.setWindowFlags(flags)
Combining Flags
You often need to combine multiple flags. You do this using the bitwise OR operator ().
Common Flags:
Flag (Qt.WindowType....) |
Description |
|---|---|
Window |
A normal window. This is the default. |
Dialog |
A window with a dialog box look and feel. Usually has a close button and a context help button. |
SplashScreen |
A splash screen. Usually has no title bar. |
Tool |
A tool window. Usually has a smaller title bar. |
WindowTitleHint |
Shows a title bar. |
WindowStaysOnTopHint |
The window stays on top of all other windows. |
FramelessWindowHint |
The window has no window frame or title bar. |
CustomizeWindowHint |
Disables some default window features (like the what's this button). |
WindowMinimizeButtonHint |
Adds a minimize button to the title bar. |
WindowMaximizeButtonHint |
Adds a maximize button to the title bar. |
WindowCloseButtonHint |
Adds a close button to the title bar. |
The Complete Workflow
- Import necessary classes.
- Create your application and main window.
- Call
setWindowFlags()with your desired flag combination on the window object. - (Crucial) Call
show()to display the window. The flags are applied when the window is shown.
Practical Examples
Let's see some common use cases.
Example 1: A Frameless Window (No Border)
This is useful for creating custom-styled applications, like a media player or a custom dialog.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
class FramelessWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Frameless Window")
self.setGeometry(100, 100, 400, 200)
# --- KEY PART ---
# Set the window to be frameless
self.setWindowFlags(Qt.FramelessWindowHint)
# Add some content to see the window
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
label = QLabel("This is a frameless window. You cannot drag it by the title bar.")
label.setAlignment(Qt.AlignCenter)
layout.addWidget(label)
central_widget.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = FramelessWindow()
window.show()
sys.exit(app.exec_())
Example 2: A Window That Stays on Top
This is great for widgets you want to keep visible while working in other applications, like a notepad or a calculator.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
class AlwaysOnTopWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Always on Top")
self.setGeometry(300, 300, 300, 150)
# --- KEY PART ---
# Combine flags: make it a normal window but also keep it on top
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
# Add content
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
label = QLabel("I will always be on top of other windows!")
label.setAlignment(Qt.AlignCenter)
layout.addWidget(label)
central_widget.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = AlwaysOnTopWindow()
window.show()
sys.exit(app.exec_())
Note: self.windowFlags() gets the current flags, and adds the new one to them. This is useful if you want to preserve other default flags.
Example 3: A Custom Title Bar (Frameless but with Min/Max/Close)
This is a more advanced example. We create a frameless window but add our own custom buttons for minimizing, maximizing, and closing.
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel)
from PyQt5.QtCore import Qt, QPoint
class CustomTitleBarWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.setWindowTitle("Custom Title Bar")
self.setGeometry(150, 150, 500, 300)
# --- Custom Title Bar ---
title_bar = QWidget()
title_bar.setStyleSheet("background-color: #2c3e50; color: white;")
title_bar.setFixedHeight(30)
title_layout = QHBoxLayout(title_bar)
title_layout.setContentsMargins(5, 0, 5, 0)
self.title_label = QLabel("My Custom App")
title_layout.addWidget(self.title_label, stretch=1)
self.min_button = QPushButton("Min")
self.max_button = QPushButton("Max")
self.close_button = QPushButton("X")
for btn in [self.min_button, self.max_button, self.close_button]:
btn.setFixedSize(30, 20)
btn.setStyleSheet("border: none; color: white;")
title_layout.addWidget(self.min_button)
title_layout.addWidget(self.max_button)
title_layout.addWidget(self.close_button)
# --- Main Content Area ---
central_widget = QWidget()
main_layout = QVBoxLayout(central_widget)
main_layout.setContentsMargins(0, 0, 0, 0)
main_layout.setSpacing(0)
main_layout.addWidget(title_bar) # Add title bar at the top
main_layout.addWidget(QLabel("This is the main content area. Drag the title bar to move me!"))
self.setCentralWidget(central_widget)
# --- Connect Buttons ---
self.close_button.clicked.connect(self.close)
self.min_button.clicked.connect(self.showMinimized)
# Maximize/Restore is a bit more complex
self.max_button.clicked.connect(self.toggle_maximize)
# --- Variables for Dragging ---
self._drag_pos = None
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton and event.pos().y() < 30: # Check if click is on title bar
self._drag_pos = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton and self._drag_pos is not None:
self.move(event.globalPos() - self._drag_pos)
event.accept()
def mouseReleaseEvent(self, event):
self._drag_pos = None
def toggle_maximize(self):
if self.isMaximized():
self.showNormal()
self.max_button.setText("Max")
else:
self.showMaximized()
self.max_button.setText("Restore")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = CustomTitleBarWindow()
window.show()
sys.exit(app.exec_())
Important Considerations
-
When to Call
setWindowFlags: You must call it before the window is displayed withshow(). Calling it on an already visible window may not work as expected or may require you to also callshow()again. -
Overriding Flags: When you call
setWindowFlags(flag), you are replacing all existing flags. If you want to add a flag to the existing ones (like in the "Always on Top" example), use the bitwise OR operator:self.setWindowFlags(self.windowFlags() | Qt.NewFlag). -
Qt Designer: If you are using Qt Designer to create your UI, you can also set window flags in the
Property Editorfor your main window widget. The property is calledwindowFlags. This is often easier for static settings.
