|  | Home · Overviews · Examples

# Window Flags Example

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.

## ControllerWindow Class Definition

    class ControllerWindow : public QWidget
    {
        Q_OBJECT

    public:
        ControllerWindow();

    private slots:
        void updatePreview();

    private:
        void createTypeGroupBox();
        void createHintsGroupBox();
        QCheckBox *createCheckBox(const QString &text);
        QRadioButton *createRadioButton(const QString &text);

        PreviewWindow *previewWindow;

        QGroupBox *typeGroupBox;
        QGroupBox *hintsGroupBox;
        QPushButton *quitButton;

        QRadioButton *windowRadioButton;
        QRadioButton *dialogRadioButton;
        QRadioButton *sheetRadioButton;
        QRadioButton *drawerRadioButton;
        QRadioButton *popupRadioButton;
        QRadioButton *toolRadioButton;
        QRadioButton *toolTipRadioButton;
        QRadioButton *splashScreenRadioButton;

        QCheckBox *msWindowsFixedSizeDialogCheckBox;
        QCheckBox *x11BypassWindowManagerCheckBox;
        QCheckBox *framelessWindowCheckBox;
        QCheckBox *windowTitleCheckBox;
        QCheckBox *windowSystemMenuCheckBox;
        QCheckBox *windowMinimizeButtonCheckBox;
        QCheckBox *windowMaximizeButtonCheckBox;
        QCheckBox *windowContextHelpButtonCheckBox;
        QCheckBox *windowShadeButtonCheckBox;
        QCheckBox *windowStaysOnTopCheckBox;
        QCheckBox *customizeWindowHintCheckBox;
    };

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.

## ControllerWindow Class Implementation

The following code example is written in c++.

    ControllerWindow::ControllerWindow()
    {
        previewWindow = new PreviewWindow(this);

        createTypeGroupBox();
        createHintsGroupBox();

        quitButton = new QPushButton(tr("&Quit"));
        connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));

        QHBoxLayout *bottomLayout = new QHBoxLayout;
        bottomLayout->addStretch();
        bottomLayout->addWidget(quitButton);

        QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->addWidget(typeGroupBox);
        mainLayout->addWidget(hintsGroupBox);
        mainLayout->addLayout(bottomLayout);
        setLayout(mainLayout);

        setWindowTitle(tr("Window Flags"));
        updatePreview();
    }

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 following code example is written in c++.

    void ControllerWindow::updatePreview()
    {
        Qt::WindowFlags flags = 0;

        if (windowRadioButton->isChecked()) {
            flags = Qt::Window;
        } else if (dialogRadioButton->isChecked()) {
            flags = Qt::Dialog;
        } else if (sheetRadioButton->isChecked()) {
            flags = Qt::Sheet;
        } else if (drawerRadioButton->isChecked()) {
            flags = Qt::Drawer;
        } else if (popupRadioButton->isChecked()) {
            flags = Qt::Popup;
        } else if (toolRadioButton->isChecked()) {
            flags = Qt::Tool;
        } else if (toolTipRadioButton->isChecked()) {
            flags = Qt::ToolTip;
        } else if (splashScreenRadioButton->isChecked()) {
            flags = Qt::SplashScreen;

        }

The `updatePreview()` slot is called whenever the user changes any of the window flags. First we create an empty Qt::WindowFlags`flags`, then we determine which one of the types that is checked and add it to `flags`.
The following code example is written in c++.

        if (msWindowsFixedSizeDialogCheckBox->isChecked())
            flags |= Qt::MSWindowsFixedSizeDialogHint;
        if (x11BypassWindowManagerCheckBox->isChecked())
            flags |= Qt::X11BypassWindowManagerHint;
        if (framelessWindowCheckBox->isChecked())
            flags |= Qt::FramelessWindowHint;
        if (windowTitleCheckBox->isChecked())
            flags |= Qt::WindowTitleHint;
        if (windowSystemMenuCheckBox->isChecked())
            flags |= Qt::WindowSystemMenuHint;
        if (windowMinimizeButtonCheckBox->isChecked())
            flags |= Qt::WindowMinimizeButtonHint;
        if (windowMaximizeButtonCheckBox->isChecked())
            flags |= Qt::WindowMaximizeButtonHint;
        if (windowContextHelpButtonCheckBox->isChecked())
            flags |= Qt::WindowContextHelpButtonHint;
        if (windowShadeButtonCheckBox->isChecked())
            flags |= Qt::WindowShadeButtonHint;
        if (windowStaysOnTopCheckBox->isChecked())
            flags |= Qt::WindowStaysOnTopHint;
        if (customizeWindowHintCheckBox->isChecked())
            flags |= Qt::CustomizeWindowHint;

        previewWindow->setWindowFlags(flags);

We also determine which of the hints that are checked, and add them to `flags` using an OR operator. We use `flags` to set the window flags for the preview window.
The following code example is written in c++.

        QPoint pos = previewWindow->pos();
        if (pos.x() < 0)
            pos.setX(0);
        if (pos.y() < 0)
            pos.setY(0);
        previewWindow->move(pos);
        previewWindow->show();
    }

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 following code example is written in c++.

    void ControllerWindow::createTypeGroupBox()
    {
        typeGroupBox = new QGroupBox(tr("Type"));

        windowRadioButton = createRadioButton(tr("Window"));
        dialogRadioButton = createRadioButton(tr("Dialog"));
        sheetRadioButton = createRadioButton(tr("Sheet"));
        drawerRadioButton = createRadioButton(tr("Drawer"));
        popupRadioButton = createRadioButton(tr("Popup"));
        toolRadioButton = createRadioButton(tr("Tool"));
        toolTipRadioButton = createRadioButton(tr("Tooltip"));
        splashScreenRadioButton = createRadioButton(tr("Splash screen"));
        windowRadioButton->setChecked(true);

        QGridLayout *layout = new QGridLayout;
        layout->addWidget(windowRadioButton, 0, 0);
        layout->addWidget(dialogRadioButton, 1, 0);
        layout->addWidget(sheetRadioButton, 2, 0);
        layout->addWidget(drawerRadioButton, 3, 0);
        layout->addWidget(popupRadioButton, 0, 1);
        layout->addWidget(toolRadioButton, 1, 1);
        layout->addWidget(toolTipRadioButton, 2, 1);
        layout->addWidget(splashScreenRadioButton, 3, 1);
        typeGroupBox->setLayout(layout);
    }

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 following code example is written in c++.

    void ControllerWindow::createHintsGroupBox()
    {
        hintsGroupBox = new QGroupBox(tr("Hints"));

        msWindowsFixedSizeDialogCheckBox =
                createCheckBox(tr("MS Windows fixed size dialog"));
        x11BypassWindowManagerCheckBox =
                createCheckBox(tr("X11 bypass window manager"));
        framelessWindowCheckBox = createCheckBox(tr("Frameless window"));
        windowTitleCheckBox = createCheckBox(tr("Window title"));
        windowSystemMenuCheckBox = createCheckBox(tr("Window system menu"));
        windowMinimizeButtonCheckBox = createCheckBox(tr("Window minimize button"));
        windowMaximizeButtonCheckBox = createCheckBox(tr("Window maximize button"));
        windowContextHelpButtonCheckBox =
                createCheckBox(tr("Window context help button"));
        windowShadeButtonCheckBox = createCheckBox(tr("Window shade button"));
        windowStaysOnTopCheckBox = createCheckBox(tr("Window stays on top"));
        customizeWindowHintCheckBox= createCheckBox(tr("Customize window"));

        QGridLayout *layout = new QGridLayout;
        layout->addWidget(msWindowsFixedSizeDialogCheckBox, 0, 0);
        layout->addWidget(x11BypassWindowManagerCheckBox, 1, 0);
        layout->addWidget(framelessWindowCheckBox, 2, 0);
        layout->addWidget(windowTitleCheckBox, 3, 0);
        layout->addWidget(windowSystemMenuCheckBox, 4, 0);
        layout->addWidget(windowMinimizeButtonCheckBox, 0, 1);
        layout->addWidget(windowMaximizeButtonCheckBox, 1, 1);
        layout->addWidget(windowContextHelpButtonCheckBox, 2, 1);
        layout->addWidget(windowShadeButtonCheckBox, 3, 1);
        layout->addWidget(windowStaysOnTopCheckBox, 4, 1);
        layout->addWidget(customizeWindowHintCheckBox, 5, 0);
        hintsGroupBox->setLayout(layout);
    }

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.
The following code example is written in c++.

    QCheckBox *ControllerWindow::createCheckBox(const QString &text)
    {
        QCheckBox *checkBox = new QCheckBox(text);
        connect(checkBox, SIGNAL(clicked()), this, SLOT(updatePreview()));
        return checkBox;
    }

The private `createCheckBox()` function is called from `createHintsGroupBox()`.

We simply create a QCheckBox with the provided text, connect it to the private `updatePreview()` slot, and return a pointer to the checkbox.
The following code example is written in c++.

    QRadioButton *ControllerWindow::createRadioButton(const QString &text)
    {
        QRadioButton *button = new QRadioButton(text);
        connect(button, SIGNAL(clicked()), this, SLOT(updatePreview()));
        return button;
    }

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

    class PreviewWindow : public QWidget
    {
        Q_OBJECT

    public:
        PreviewWindow(QWidget *parent = 0);

        void setWindowFlags(Qt::WindowFlags flags);

    private:
        QTextEdit *textEdit;
        QPushButton *closeButton;
    };

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.

## PreviewWindow Class Implementation

The following code example is written in c++.

    PreviewWindow::PreviewWindow(QWidget *parent)
        : QWidget(parent)
    {
        textEdit = new QTextEdit;
        textEdit->setReadOnly(true);
        textEdit->setLineWrapMode(QTextEdit::NoWrap);

        closeButton = new QPushButton(tr("&Close"));
        connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

        QVBoxLayout *layout = new QVBoxLayout;
        layout->addWidget(textEdit);
        layout->addWidget(closeButton);
        setLayout(layout);

        setWindowTitle(tr("Preview"));
    }

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.
The following code example is written in c++.

    void PreviewWindow::setWindowFlags(Qt::WindowFlags flags)
    {
        QWidget::setWindowFlags(flags);

        QString text;

        Qt::WindowFlags type = (flags & Qt::WindowType_Mask);
        if (type == Qt::Window) {
            text = "Qt::Window";
        } else if (type == Qt::Dialog) {
            text = "Qt::Dialog";
        } else if (type == Qt::Sheet) {
            text = "Qt::Sheet";
        } else if (type == Qt::Drawer) {
            text = "Qt::Drawer";
        } else if (type == Qt::Popup) {
            text = "Qt::Popup";
        } else if (type == Qt::Tool) {
            text = "Qt::Tool";
        } else if (type == Qt::ToolTip) {
            text = "Qt::ToolTip";
        } else if (type == Qt::SplashScreen) {
            text = "Qt::SplashScreen";
        }

        if (flags & Qt::MSWindowsFixedSizeDialogHint)
            text += "\n| Qt::MSWindowsFixedSizeDialogHint";
        if (flags & Qt::X11BypassWindowManagerHint)
            text += "\n| Qt::X11BypassWindowManagerHint";
        if (flags & Qt::FramelessWindowHint)
            text += "\n| Qt::FramelessWindowHint";
        if (flags & Qt::WindowTitleHint)
            text += "\n| Qt::WindowTitleHint";
        if (flags & Qt::WindowSystemMenuHint)
            text += "\n| Qt::WindowSystemMenuHint";
        if (flags & Qt::WindowMinimizeButtonHint)
            text += "\n| Qt::WindowMinimizeButtonHint";
        if (flags & Qt::WindowMaximizeButtonHint)
            text += "\n| Qt::WindowMaximizeButtonHint";
        if (flags & Qt::WindowContextHelpButtonHint)
            text += "\n| Qt::WindowContextHelpButtonHint";
        if (flags & Qt::WindowShadeButtonHint)
            text += "\n| Qt::WindowShadeButtonHint";
        if (flags & Qt::WindowStaysOnTopHint)
            text += "\n| Qt::WindowStaysOnTopHint";
        if (flags & Qt::CustomizeWindowHint)
            text += "\n| Qt::CustomizeWindowHint";

        textEdit->setPlainText(text);
    }

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.