このページでは

ウィンドウフラグの例

「ウィンドウフラグ」の例では、Qtで利用可能なウィンドウフラグの使用方法を示しています。

ウィンドウフラグには、「タイプ」と「ヒント」の2種類があります。タイプは、ウィジェットのウィンドウシステムに関するさまざまなプロパティを指定するために使用されます。ウィジェットに指定できるタイプは1つだけで、デフォルトはQt::Widget です。一方、ウィジェットには0個以上のヒントを指定できます。ヒントは、トップレベルウィンドウの外観をカスタマイズするために使用されます。

ウィジェットのフラグは、フラグの論理和(OR)を格納するQt::WindowFlags 型に保存されます。

コントローラーウィンドウとプレビューウィンドウ

「Window Flags」例のスクリーンショット

このサンプルは、2つのクラスで構成されています:

  • ControllerWindow は、ユーザーが利用可能なウィンドウフラグの中から選択できるようにし、その効果を別のプレビューウィンドウに表示するメインアプリケーションウィジェットです。
  • PreviewWindow は、現在設定されているウィンドウフラグの名前を読み取り専用のテキストエディタに表示するカスタムウィジェットです。

まず、ControllerWindow クラスについて確認し、次にPreviewWindow クラスについて見ていきます。

ControllerWindowクラスの定義

class ControllerWindow : public QWidget
{
    Q_OBJECT

public:
    ControllerWindow(QWidget *parent = nullptr);

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 *windowNoShadowCheckBox;
    QCheckBox *windowTitleCheckBox;
    QCheckBox *windowSystemMenuCheckBox;
    QCheckBox *windowMinimizeButtonCheckBox;
    QCheckBox *windowMaximizeButtonCheckBox;
    QCheckBox *windowCloseButtonCheckBox;
    QCheckBox *windowContextHelpButtonCheckBox;
    QCheckBox *windowShadeButtonCheckBox;
    QCheckBox *windowStaysOnTopCheckBox;
    QCheckBox *windowStaysOnBottomCheckBox;
    QCheckBox *customizeWindowHintCheckBox;
};

ControllerWindow クラスはQWidget を継承しています。このウィジェットを使用すると、ユーザーは利用可能なウィンドウフラグの中から選択でき、その効果が別のプレビューウィンドウに表示されます。

ユーザーがウィンドウフラグを変更するたびにプレビューウィンドウを更新するために、プライベートな `updatePreview() ` スロットを宣言します。

また、コンストラクタを簡略化するために、いくつかのプライベート関数を宣言しています。createTypeGroupBox() 関数を呼び出し、プライベート関数createButton() を使用して利用可能な各ウィンドウタイプに対応するラジオボタンを作成し、それらをグループボックス内にまとめます。同様に、createHintsGroupBox() 関数を使用して、プライベート関数createCheckBox() を活用し、利用可能な各ヒントに対応するチェックボックスを作成します。

さまざまなラジオボタンやチェックボックスに加え、現在選択されているウィンドウフラグの効果を表示するための関連するPreviewWindow も必要です。

さまざまなフラグの種類とヒントが表示されたコントローラーウィンドウ

ControllerWindow クラスの実装

ControllerWindow::ControllerWindow(QWidget*parent)
    : QWidget(parent)
{
    previewWindow= newPreviewWindow(this);

    createTypeGroupBox();
    createHintsGroupBox();

    quitButton= newQPushButton(tr("&Quit"));
    connect(quitButton, &QPushButton::clicked,
            qApp, &QCoreApplication::quit);

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

    QHBoxLayout*mainLayout = newQHBoxLayout;
    mainLayout->addWidget(typeGroupBox);
    mainLayout->addWidget(hintsGroupBox);
    mainLayout->addLayout(bottomLayout);
    setLayout(mainLayout);

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

コンストラクタでは、まずプレビューウィンドウを作成します。次に、プライベート関数createTypeGroupBox() およびcreateHintsGroupBox() を使用して、利用可能なウィンドウフラグを含むグループボックスを作成します。さらに、Quit ボタンを作成します。このボタンと伸縮可能なスペースを別のレイアウトに配置し、ボタンがWindowFlag ウィジェットの右下隅に表示されるようにします。

最後に、ボタンのレイアウトと2つのグループボックスをQVBoxLayout に追加し、ウィンドウのタイトルを設定して、updatePreview() スロットを使用してプレビューウィンドウを更新します。

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

    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;

updatePreview() スロットは、ユーザーがウィンドウのフラグのいずれかを変更するたびに呼び出されます。まず、空のQt::WindowFlags flags を作成し、次にどのタイプがチェックされているかを判定して、それをflags に追加します。

    if (msWindowsFixedSizeDialogCheckBox->isChecked())
        flags |= Qt::MSWindowsFixedSizeDialogHint;
    if (x11BypassWindowManagerCheckBox->isChecked())
        flags |= Qt::X11BypassWindowManagerHint;
    if (framelessWindowCheckBox->isChecked())
        flags |= Qt::FramelessWindowHint;
    if (windowNoShadowCheckBox->isChecked())
        flags |= Qt::NoDropShadowWindowHint;
    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 (windowCloseButtonCheckBox->isChecked())
        flags |= Qt::WindowCloseButtonHint;
    if (windowContextHelpButtonCheckBox->isChecked())
        flags |= Qt::WindowContextHelpButtonHint;
    if (windowShadeButtonCheckBox->isChecked())
        flags |= Qt::WindowShadeButtonHint;
    if (windowStaysOnTopCheckBox->isChecked())
        flags |= Qt::WindowStaysOnTopHint;
    if (windowStaysOnBottomCheckBox->isChecked())
        flags |= Qt::WindowStaysOnBottomHint;
    if (customizeWindowHintCheckBox->isChecked())
        flags |= Qt::CustomizeWindowHint;

    previewWindow->setWindowFlags(flags);

また、チェックされているヒントを特定し、OR演算子を使用してそれらをflags に追加します。flags を使用して、プレビューウィンドウのウィンドウフラグを設定します。

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

プレビューウィンドウの位置を調整します。これを行う理由は、ウィンドウのフレームを操作すると、一部のプラットフォームでは、意図せずウィンドウの位置が変更されてしまう可能性があるためです。ウィンドウが画面の左上隅にある場合、ウィンドウの一部が表示されない可能性があります。 そこで、このような事態が発生した場合でも、ウィンドウが画面の境界内に収まるよう、ウィジェットの位置を調整します。最後に、QWidget::show() を呼び出して、プレビューウィンドウが確実に表示されるようにします。

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

プライベート関数 `createTypeGroupBox() ` は、コンストラクタから呼び出されます。

まずグループボックスを作成し、次にウィンドウフラグで指定可能な各タイプについて、(プライベート関数createRadioButton() を使用して)ラジオボタンを作成します。初期状態で適用されるタイプとしてQt::Window を指定します。ラジオボタンをQGridLayout に配置し、そのレイアウトをグループボックスに適用します。

デフォルトのQt::Widget タイプは含めません。その理由は、このタイプが他のタイプとは動作が多少異なるためです。ウィジェットのタイプが指定されておらず、かつ親ウィジェットがない場合、そのウィジェットはウィンドウとなります。しかし、親ウィジェットがある場合は、標準の子ウィジェットとなります。 他のタイプはすべてトップレベルウィンドウであり、ヒントはトップレベルウィンドウにのみ影響するため、Qt::Widget タイプは採用していません。

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"));
    windowNoShadowCheckBox = createCheckBox(tr("No drop shadow"));
    windowTitleCheckBox = createCheckBox(tr("Window title"));
    windowSystemMenuCheckBox = createCheckBox(tr("Window system menu"));
    windowMinimizeButtonCheckBox = createCheckBox(tr("Window minimize button"));
    windowMaximizeButtonCheckBox = createCheckBox(tr("Window maximize button"));
    windowCloseButtonCheckBox = createCheckBox(tr("Window close button"));
    windowContextHelpButtonCheckBox =
            createCheckBox(tr("Window context help button"));
    windowShadeButtonCheckBox = createCheckBox(tr("Window shade button"));
    windowStaysOnTopCheckBox = createCheckBox(tr("Window stays on top"));
    windowStaysOnBottomCheckBox = createCheckBox(tr("Window stays on bottom"));
    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(windowNoShadowCheckBox, 3, 0);
    layout->addWidget(windowTitleCheckBox, 4, 0);
    layout->addWidget(windowSystemMenuCheckBox, 5, 0);
    layout->addWidget(customizeWindowHintCheckBox, 6, 0);
    layout->addWidget(windowMinimizeButtonCheckBox, 0, 1);
    layout->addWidget(windowMaximizeButtonCheckBox, 1, 1);
    layout->addWidget(windowCloseButtonCheckBox, 2, 1);
    layout->addWidget(windowContextHelpButtonCheckBox, 3, 1);
    layout->addWidget(windowShadeButtonCheckBox, 4, 1);
    layout->addWidget(windowStaysOnTopCheckBox, 5, 1);
    layout->addWidget(windowStaysOnBottomCheckBox, 6, 1);
    hintsGroupBox->setLayout(layout);
}

プライベート関数 `createHintsGroupBox() ` も、コンストラクタから呼び出されます。

ここでも、まず最初にグループボックスを作成します。次に、ウィンドウフラグに含まれる利用可能なヒントごとに、プライベート関数 `createCheckBox() ` を使用してチェックボックスを作成します。これらのチェックボックスを `QGridLayout ` に格納し、そのレイアウトをグループボックスに適用します。

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

プライベート関数 `createCheckBox() ` は、`createHintsGroupBox()` から呼び出されます。

指定されたテキストを使用してQCheckBox を作成し、それをupdatePreview() のプライベートスロットに接続し、チェックボックスへのポインタを返すだけです。

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

非公開のcreateRadioButton() 関数内では、指定されたテキストを使用してQRadioButton を作成し、非公開のupdatePreview() スロットに接続します。この関数はcreateTypeGroupBox() から呼び出され、ボタンへのポインタを返します。

PreviewWindowクラスの定義

class PreviewWindow : public QWidget
{
    Q_OBJECT

public:
    PreviewWindow(QWidget *parent = nullptr);

    void setWindowFlags(Qt::WindowFlags flags);

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

PreviewWindow クラスはQWidget を継承しています。これは、現在設定されているウィンドウフラグの名前を読み取り専用のテキストエディタに表示するカスタムウィジェットです。また、ウィンドウを閉じるためのQPushbuttonも備えています。

コンストラクタを再実装してClose ボタンとテキストエディタを作成し、QWidget::setWindowFlags()関数を再実装してウィンドウフラグの名前を表示するようにしています。

ウィンドウフラグを表示するプレビューウィンドウ

PreviewWindow クラスの実装

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

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

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

    setWindowTitle(tr("Preview"));
}

コンストラクタでは、まずQTextEdit を作成し、それが読み取り専用であることを確認します。

また、QTextEdit::setLineWrapMode() 関数を使用して、テキストエディタでの改行を一切禁止します。その結果、ウィンドウフラグの名前がエディタの幅を超えると、水平スクロールバーが表示されます。これは、表示されるテキストを組み込みの改行で構成しているため、妥当な解決策です。もし改行が保証されていない場合は、別のQTextEdit::LineWrapMode を使用するほうが理にかなっているかもしれません。

次に、Close ボタンを作成し、ウィンドウのタイトルを設定する前に、両方のウィジェットをQVBoxLayout 内に配置します。

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::NoDropShadowWindowHint)
        text += "\n| Qt::NoDropShadowWindowHint";
    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::WindowCloseButtonHint)
        text += "\n| Qt::WindowCloseButtonHint";
    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::WindowStaysOnBottomHint)
        text += "\n| Qt::WindowStaysOnBottomHint";
    if (flags & Qt::CustomizeWindowHint)
        text += "\n| Qt::CustomizeWindowHint";

    textEdit->setPlainText(text);
}

setWindowFlags() 関数を再実装する際、まずQWidget::setWindowFlags()関数を使用してウィジェットのフラグを設定します。次に、利用可能なウィンドウフラグを順に処理し、flags パラメータに一致するフラグの名前を含むテキストを作成します。最後に、そのテキストをウィジェットのテキストエディタに表示します。

サンプルプロジェクト @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.