Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Window Flags Example

Files:

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.

Screenshot of the Window Flags example

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;
    };

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.

Screenshot of the Controller Window

ControllerWindow Class Implementation

    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.

    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.

        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;

        previewWindow->setWindowFlags(flags);
        previewWindow->show();

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, and call QWidget::show() to make sure the preview window is visible.

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

Finally, 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.

    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.

    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"));

        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);
        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.

    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.

    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.

Screenshot of the Preview Window

PreviewWindow Class Implementation

    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.

    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";

        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.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 23
  2. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 46
  3. Une nouvelle ère d'IHM 3D pour les automobiles, un concept proposé par Digia et implémenté avec Qt 3
  4. Qt Creator 2.5 est sorti en beta, l'EDI supporte maintenant plus de fonctionnalités de C++11 2
  5. PySide devient un add-on Qt et rejoint le Qt Project et le modèle d'open gouvernance 1
  6. Vingt sociétés montrent leurs décodeurs basés sur Qt au IPTV World Forum, en en exploitant diverses facettes (déclaratif, Web, widgets) 0
  7. Thread travailleur avec Qt en utilisant les signaux et les slots, un article de Christophe Dumez traduit par Thibaut Cuvelier 1
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 50
  4. Les développeurs détestent-ils les antivirus ? Un programmeur manifeste sa haine envers ces solutions de sécurité 23
  5. «Le projet de loi des droits du développeur» : quelles conditions doivent remplir les entreprises pour que le développeur puisse réussir ? 46
  6. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  7. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
Page suivante

Le Qt Developer Network au hasard

Logo

Installation de PySide : binaires et compilation

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.1
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web