WindowTitleBar Class
The WindowTitleBar class provides customization options for the title bar of top-level windows. More...
| Header: | #include <WindowTitleBar> |
| Inherits: | QObject |
Public Types
| enum | BlurBehindWindowEffect { Disabled, Acrylic, Mica } |
| enum | SystemButton { SysMenuButton, HelpButton, BackButton, DarkModeButton, SettingsButton } |
| flags | SystemButtons |
| enum | WidgetAlign { AlignLeft, AlignRight, AlignClient } |
Public Functions
| WindowTitleBar::BlurBehindWindowEffect | blurBehindWindowEffect() const |
| int | borderThickness() const |
| bool | extendViewIntoTitleBar() const |
| void | hide() |
| bool | isDarkMode() const |
| bool | isSystemButtonVisible(WindowTitleBar::SystemButton button) const |
| bool | isVisible() const |
| void | setBlurBehindWindowEffect(WindowTitleBar::BlurBehindWindowEffect effect) |
| void | setBlurBehindWindowEnabled(bool enabled) |
| void | setBorderThickness(int thickness) |
| void | setDarkMode(bool darkMode) |
| void | setExtendViewIntoTitleBar(bool extend) |
| void | setSystemButtonVisible(WindowTitleBar::SystemButton button, bool visible) |
| void | setTitleHeight(int height) |
| void | setVisible(bool visible) |
| void | setWidget(QWidget *widget, WindowTitleBar::WidgetAlign align = AlignLeft) |
| void | show() |
| int | titleHeight() const |
| void | update() |
| QWidget * | widget() const |
Signals
| void | backRequested() |
| void | darkModeActivated(bool darkMode) |
| void | showHelp() |
| void | showSettings() |
Detailed Description
The WindowTitleBar class allows developers to customize the appearance and behavior of the title bar for top-level windows. It supports features such as adding or hiding buttons, applying blur effects, and overriding specific style properties (e.g., frame thickness or title bar color). While most settings are derived from QStyle, certain properties can be customized directly through this class.
Key Features
- Supports adding or hiding custom buttons in the title bar (e.g., system menu, back button, dark mode toggle).
- Allows applying blur effects to the window background, including acrylic and mica effects.
- Enables extending the client area into the title bar for a seamless, modern design.
- Provides overrides for specific style properties, such as frame thickness and title bar color.
- Integrates with QStyle for consistent theming and styling across the application.
Blur Effects
Blur Effects
The WindowTitleBar class supports the following blur effects for the window background:
Disabled: No blur effect is applied.Acrylic: Applies an acrylic blur effect, creating a frosted glass appearance that blends with the desktop wallpaper.Mica: Applies a mica blur effect, blending the window background with both the system theme colors and the blurred desktop wallpaper (available only on Windows 11).
These effects can be applied using the WindowTitleBar::setBlurBehindWindowEffect method.
Usage Example
Here's an example of how to use the WindowTitleBar class to customize the title bar of a top-level window:
#include <DevMachines/QtitanBase> #include <DevMachines/QtitanStyle> int main(int argc, char* argv[]) { QApplication app(argc, argv); //Create a style for the app qApp->setStyle(new WindowsUIStyle()); // Create a top-level window QMainWindow window; // Customize the title bar WindowTitleBar* titleBar = Qtitan::WindowTitleBar::get(&window); // Enable acrylic blur effect titleBar->setBlurBehindWindowEffect(WindowTitleBar::Acrylic); // Extend the client area into the title bar titleBar->setExtendViewIntoTitleBar(true); // Add custom buttons titleBar->setSystemButtonVisible(WindowTitleBar::DarkModeButton, true); titleBar->setSystemButtonVisible(WindowTitleBar::SettingsButton, true); //Show the title bar titleBar->show(); // Show the window window.resize(800, 600); window.show(); return app.exec(); }
Member Type Documentation
enum WindowTitleBar::BlurBehindWindowEffect
Defines the blur effects that can be applied behind the window to enhance the visual appearance.
| Constant | Value | Description |
|---|---|---|
WindowTitleBar::Disabled | 0 | No blur effect is applied. The window background is rendered as-is. |
WindowTitleBar::Acrylic | 1 | Applies an acrylic blur effect. This creates a frosted glass appearance, blending the window with the desktop background. |
WindowTitleBar::Mica | 2 | Applies a Mica blur effect. This effect is similar to acrylic but blending the window background with the system theme colors and the blurred desktop wallpaper, providing a more subtle and modern look. Note: The Mica effect is only available on Windows 11. The Acrylic effect is supported on Windows 10 and later. |
enum WindowTitleBar::SystemButton
flags WindowTitleBar::SystemButtons
Defines the system buttons that can be added to the title bar of the application window. Each button is represented by a bit flag, allowing multiple buttons to be combined using bitwise operations.
| Constant | Value | Description |
|---|---|---|
WindowTitleBar::SysMenuButton | 0x0001 | Represents the system menu button (e.g., the application icon). This button is typically used to display a context menu or perform actions related to the application. |
WindowTitleBar::HelpButton | 0x0002 | Represents the help button (e.g., the qustion sign). This button is typically used to show a context help for the application. |
WindowTitleBar::BackButton | 0x0004 | Represents the "Back" button. This button is used to navigate to the previous screen or state in the application. |
WindowTitleBar::DarkModeButton | 0x0008 | Represents the dark mode toggle button. This button allows the user to switch between light and dark themes in the application. |
WindowTitleBar::SettingsButton | 0x0010 | Represents the settings button. This button typically opens a dialog or panel for configuring application settings. |
The SystemButtons type is a typedef for QFlags<SystemButton>. It stores an OR combination of SystemButton values.
enum WindowTitleBar::WidgetAlign
Defines the alignment options for widgets inside the title bar.
| Constant | Value | Description |
|---|---|---|
WindowTitleBar::AlignLeft | 0 | Aligns the widget to the left side of the title bar. Typically used for system menu buttons or navigation controls. |
WindowTitleBar::AlignRight | 1 | Aligns the widget to the right side of the title bar. Commonly used for action buttons like "Settings" or "Dark Mode." |
WindowTitleBar::AlignClient | 2 | Centers the widget within the title bar. This option is useful for displaying titles or other central elements. |
Member Function Documentation
[signal] void WindowTitleBar::backRequested()
This signal is emitted when the user clicks the button WindowTitleBar::BackButton in the window title bar.
WindowTitleBar::BlurBehindWindowEffect WindowTitleBar::blurBehindWindowEffect() const
Returns the currently applied blur effect for the window. The return value is one of the enumerators from the BlurBehindWindowEffect enumeration.
See also setBlurBehindWindowEffect().
int WindowTitleBar::borderThickness() const
Returns the window non-client area frame thickness.
See also setBorderThickness().
[signal] void WindowTitleBar::darkModeActivated(bool darkMode)
This signal is emitted when the user interacts with the WindowTitleBar::DarkModeButton to toggle between light and dark modes.
// Connect the darkModeActivated signal to a slot connect(windowTitleBar, &WindowTitleBar::darkModeActivated, [](bool darkMode) { if (darkMode) { qDebug() << "Switching to dark mode."; // Apply dark mode logic (e.g., update the palette or stylesheet) QPalette darkPalette; darkPalette.setColor(QPalette::Window, QColor(53, 53, 53)); darkPalette.setColor(QPalette::WindowText, Qt::white); qApp->setPalette(darkPalette); } else { qDebug() << "Switching to light mode."; // Apply light mode logic QPalette lightPalette; lightPalette.setColor(QPalette::Window, Qt::white); lightPalette.setColor(QPalette::WindowText, Qt::black); qApp->setPalette(lightPalette); } });
bool WindowTitleBar::extendViewIntoTitleBar() const
Returns whether the application's client area is extended into the title bar.
See also setExtendViewIntoTitleBar().
void WindowTitleBar::hide()
Hides the entire title bar. This method is equivalent to calling WindowTitleBar::setVisible(false).
bool WindowTitleBar::isDarkMode() const
Checks the current visual state of the WindowTitleBar::DarkModeButton on the title bar. Returns true if the WindowTitleBar::DarkModeButton is currently displayed in a way that indicates dark mode (e.g., using a moon icon or a dark-themed appearance). Returns false if the button is displayed in a way that indicates light mode (e.g., using a sun icon or a light-themed appearance).
bool WindowTitleBar::isSystemButtonVisible(WindowTitleBar::SystemButton button) const
Checks whether a specific system button in the title bar is currently visible. Returns true if the specified button is currently visible in the title bar. Returns false if the specified button is not visible.
bool WindowTitleBar::isVisible() const
Checks whether a custom title bar is currently visible.
void WindowTitleBar::setBlurBehindWindowEffect(WindowTitleBar::BlurBehindWindowEffect effect)
Sets the blur effect for the window. The effect is specified using one of the enumerators from the BlurBehindWindowEffect enumeration.
See also blurBehindWindowEffect().
void WindowTitleBar::setBlurBehindWindowEnabled(bool enabled)
Enables or disables the blur effect for the window. When enabled, the Acrylic blur effect is applied by default.
void WindowTitleBar::setBorderThickness(int thickness)
Sets the frame thickness of the window non-client area. If the value is not specified, then the thickness will be equal to the thickness of the system window frame. If the styledFrame() property returns true, the border thickness will be retrieved from the window style using the QStyle::PM_MdiSubWindowFrameWidth metric.
See also borderThickness().
void WindowTitleBar::setDarkMode(bool darkMode)
Updates the appearance of the WindowTitleBar::DarkModeButton to reflect whether the application is in dark mode or light mode.
Parameter darkMode: true: Updates the button's appearance to indicate that dark mode is active. false: Updates the button's appearance to indicate that light mode is active. This method does not directly switch the application's theme. Instead, it updates the visual state of the WindowTitleBar::DarkModeButton (e.g., changing its icon or style) to match the current theme. The actual theme-switching logic must be implemented separately, and this method should be called after the theme has been changed to ensure the button's appearance is synchronized.
See also isDarkMode().
void WindowTitleBar::setExtendViewIntoTitleBar(bool extend)
Enables or disables extending the application's client area into the title bar. The client area of the window is extended into the title bar, making it part of the application's drawable and interactive region. This allows for custom rendering and behavior in the title bar area. When extend is set to true, the client area expands into the title bar region. The title bar is rendered on top of the client area, enabling a seamless and modern design. This approach is particularly useful for saving user space by utilizing the otherwise unused area of the title bar. Developers must ensure that no interactive UI elements (e.g., buttons, text fields) are placed under the title bar's system buttons (e.g., minimize, maximize, close) or custom buttons (e.g., WindowTitleBar::DarkModeButton).
Usage
The primary goal of extending the client area into the title bar is to save screen space by leveraging the unused area of the title bar. This design approach aligns with modern UI paradigms, such as Fluent Design, which emphasize minimalism and efficient use of available space. When extend is set to false, the system regains control of the title bar, reverting it to the default appearance and behavior.
See also extendViewIntoTitleBar().
void WindowTitleBar::setSystemButtonVisible(WindowTitleBar::SystemButton button, bool visible)
Sets the visibility of a specific system button in the title bar. When visible is set to true, the specified button becomes part of the title bar and can be interacted with by the user. When visible is set to false, the button is removed from the visible layout of the title bar but remains available for future use if re-enabled.
// Make the Dark Mode button visible windowTitleBar->setSystemButtonVisible(WindowTitleBar::DarkModeButton, true); // Hide the Back button windowTitleBar->setSystemButtonVisible(WindowTitleBar::BackButton, false);
See also isSystemButtonVisible().
void WindowTitleBar::setTitleHeight(int height)
Sets the height of the title bar (top part of the window non-client area). If the value is not specified, then the height of the title bar will be equal to the height of the system title bar. If the styledFrame() property returns true, the height of the title bar will be retrieved from the window style using the QStyle::PM_TitleBarHeight metric.
See also titleHeight().
void WindowTitleBar::setVisible(bool visible)
Sets the visibility of the entire title bar to visible.
See also isVisible().
void WindowTitleBar::setWidget(QWidget *widget, WindowTitleBar::WidgetAlign align = AlignLeft)
Adds a custom widget to the title bar and positions it according to the specified alignment.
Parameters:
widget: The widget to add to the title bar. This can be any subclass of QWidget, such as a button, label, or other UI component. If widget is nullptr, any previously added widget is removed from the title bar. align: Specifies the alignment of the widget within the title bar. Possible values are:
Left: Positions the widget on the left side of the title bar, near the system menu or window icon.Right: Positions the widget on the right side of the title bar, near the minimize, maximize, and close buttons.Center: Positions the widget in the center of the title bar, typically alongside the window title.
See also widget().
void WindowTitleBar::show()
Makes the title bar visible. This method is equivalent to calling WindowTitleBar::setVisible(true).
[signal] void WindowTitleBar::showHelp()
This signal is emitted when the user clicks the regular help button in the window title bar.
[signal] void WindowTitleBar::showSettings()
This signal is emitted when the user clicks the button WindowTitleBar::SettingsButton in the window title bar.
int WindowTitleBar::titleHeight() const
Returns the height of the title bar.
See also setTitleHeight().
void WindowTitleBar::update()
Updates for repaint the non-client area of the window.
QWidget *WindowTitleBar::widget() const
Returns a pointer to the QWidget that was previously added using WindowTitleBar::setWidget.
See also setWidget().