창 플래그 예제
창 플래그 예제는 Qt에서 사용할 수 있는 창 플래그를 사용하는 방법을 보여줍니다.
창 플래그는 타입 또는 힌트 중 하나입니다. 유형은 위젯의 다양한 창 시스템 속성을 지정하는 데 사용됩니다. 위젯은 하나의 타입만 가질 수 있으며 기본값은 Qt::Widget 입니다. 그러나 위젯은 0개 이상의 힌트를 가질 수 있습니다. 힌트는 최상위 창 모양을 사용자 지정하는 데 사용됩니다.
위젯의 플래그는 플래그의 OR 조합을 저장하는 Qt::WindowFlags 유형에 저장됩니다.
창 플래그 예제 스크린샷
이 예제는 두 개의 클래스로 구성되어 있습니다:
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 *부모) : QWidget(parent) { previewWindow = new PreviewWindow(this); createTypeGroupBox(); createHintsGroupBox(); quitButton = new QPushButton(tr("&Quit")); connect(quitButton, &(QPushButton::클릭됨, qApp, &QCoreApplication::quit); QHBoxLayout *bottomLayout = new QHBoxLayoutbottomLayout->addStretch(); bottomLayout->addWidget(quitButton); QHBoxLayout *mainLayout = new QHBoxLayoutmainLayout->addWidget(typeGroupBox); mainLayout->addWidget(hintsGroupBox); mainLayout->addLayout(bottomLayout); setLayout(mainLayout); setWindowTitle(tr("Window Flags")); updatePreview(); }
생성자에서 먼저 미리보기 창을 생성합니다. 그런 다음 비공개 createTypeGroupBox()
및 createHintsGroupBox()
함수를 사용하여 사용 가능한 창 플래그가 포함된 그룹 상자를 만듭니다. 또한 Quit 버튼을 만듭니다. 버튼과 스트레처블 공간을 별도의 레이아웃에 배치하여 WindowFlag
위젯의 오른쪽 하단 모서리에 버튼이 나타나도록 합니다.
마지막으로 버튼의 레이아웃과 두 개의 그룹 상자를 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->show(); }
미리보기 창의 위치를 조정합니다. 이렇게 하는 이유는 일부 플랫폼에서는 창 프레임을 가지고 장난을 치면 창 위치가 뒤에서 변경될 수 있기 때문입니다. 창이 화면 왼쪽 상단에 있는 경우 창의 일부가 보이지 않을 수 있습니다. 따라서 이 경우 위젯의 위치를 조정하여 창이 화면 경계 내에서 움직이도록 합니다. 마지막으로 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 을 상속합니다. 이 클래스는 현재 설정된 창 플래그의 이름을 읽기 전용 텍스트 편집기에 표시하는 사용자 정의 위젯입니다. 또한 창을 닫는 QPush 버튼도 제공됩니다.
생성자를 다시 구현하여 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
매개변수와 일치하는 플래그의 이름이 포함된 텍스트를 만듭니다. 마지막으로 위젯 텍스트 편집기에 텍스트를 표시합니다.
© 2025 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.