The Window Flags example shows how to use the window flags available in Qt.

A window flag is either a type or a hint. A type is used to specify various window-system properties for the widget. A widget can only have one type, and the default is Qt::Widget. However, a widget can have zero or more hints. The hints are used to customize the appearance of top-level windows.

A widget's flags are stored in a Qt::WindowFlags type which stores an OR combination of the flags.

The example consists of two classes:

ControllerWindow is the main application widget that allows the user to choose among the available window flags, and displays the effect on a separate preview window.

PreviewWindow is a custom widget displaying the name of its currently set window flags in a read-only text editor.

We will start by reviewing the ControllerWindow class, then we will take a look at the PreviewWindow class.

The ControllerWindow class inherits QWidget. The widget allows the user to choose among the available window flags, and displays the effect on a separate preview window.

We declare a private updatePreview() slot to refresh the preview window whenever the user changes the window flags.

We also declare several private functions to simplify the constructor: We call the createTypeGroupBox() function to create a radio button for each available window type, using the private createButton() function, and gather them within a group box. In a similar way we use the createHintsGroupBox() function to create a check box for each available hint, using the private createCheckBox() function.

In addition to the various radio buttons and checkboxes, we need an associated PreviewWindow to show the effect of the currently chosen window flags.

In the constructor we first create the preview window. Then we create the group boxes containing the available window flags using the private createTypeGroupBox() and createHintsGroupBox() functions. In addition we create a Quit button. We put the button and a stretchable space in a separate layout to make the button appear in the WindowFlag widget's right bottom corner.

Finally, we add the button's layout and the two goup boxes to a QVBoxLayout, set the window title and refresh the preview window using the updatePreview() slot.

The updatePreview() slot is called whenever the user changes any of the window flags. First we create an empty Qt::WindowFlagsflags, then we determine which one of the types that is checked and add it to flags.

We adjust the position of the preview window. The reason we do that, is that playing around with the window's frame may on some platforms cause the window's position to be changed behind our back. If a window is located in the upper left corner of the screen, parts of the window may not be visible. So we adjust the widget's position to make sure that, if this happens, the window is moved within the screen's boundaries. Finally, we call QWidget::show() to make sure the preview window is visible.

The private createTypeGroupBox() function is called from the constructor.

First we create a group box, and then we create a radio button (using the private createRadioButton() function) for each of the available types among the window flags. We make Qt::Window the initially applied type. We put the radio buttons into a QGridLayout and install the layout on the group box.

We do not include the default Qt::Widget type. The reason is that it behaves somewhat different than the other types. If the type is not specified for a widget, and it has no parent, the widget is a window. However, if it has a parent, it is a standard child widget. The other types are all top-level windows, and since the hints only affect top-level windows, we abandon the Qt::Widget type.

The private createHintsGroupBox() function is also called from the constructor.

Again, the first thing we do is to create a group box. Then we create a checkbox, using the private createCheckBox() function, for each of the available hints among the window flags. We put the checkboxes into a QGridLayout and install the layout on the group box.

In the private createRadioButton() function it is a QRadioButton we create with the provided text, and connect to the private updatePreview() slot. The function is called from createTypeGroupBox(), and returns a pointer to the button.

PreviewWindow Class Definition

The PreviewWindow class inherits QWidget. It is a custom widget that displays the names of its currently set window flags in a read-only text editor. It is also provided with a QPushbutton that closes the window.

We reimplement the constructor to create the Close button and the text editor, and the QWidget::setWindowFlags() function to display the names of the window flags.

In the constructor, we first create a QTextEdit and make sure that it is read-only.

We also prohibit any line wrapping in the text editor using the QTextEdit::setLineWrapMode() function. The result is that a horizontal scrollbar appears when a window flag's name exceeds the width of the editor. This is a reasonable solution since we construct the displayed text with built-in line breaks. If no line breaks were guaranteed, using another QTextEdit::LineWrapMode would perhaps make more sense.

Then we create the Close button, and put both the widgets into a QVBoxLayout before we set the window title.

In our reimplementation of the setWindowFlags() function, we first set the widgets flags using the QWidget::setWindowFlags() function. Then we run through the available window flags, creating a text that contains the names of the flags that matches the flags parameter. Finally, we display the text in the widgets text editor.