QMessageBox Class

QMessageBox 클래스는 사용자에게 정보를 제공하거나 사용자에게 질문하고 답변을 받기 위한 모달 대화 상자를 제공합니다. 더 보기...

헤더: #include <QMessageBox>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
상속합니다: QDialog

공용 유형

enum ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ActionRole, …, ResetRole }
enum Icon { NoIcon, Question, Information, Warning, Critical }
(since 6.6) enum class Option { DontUseNativeDialog }
flags Options
enum StandardButton { Ok, Open, Save, Cancel, Close, …, ButtonMask }
flags StandardButtons

속성

공용 기능

QMessageBox(QWidget *parent = nullptr)
QMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)
virtual ~QMessageBox()
void addButton(QAbstractButton *button, QMessageBox::ButtonRole role)
QPushButton *addButton(QMessageBox::StandardButton button)
QPushButton *addButton(const QString &text, QMessageBox::ButtonRole role)
QAbstractButton *button(QMessageBox::StandardButton which) const
QMessageBox::ButtonRole buttonRole(QAbstractButton *button) const
QList<QAbstractButton *> buttons() const
QCheckBox *checkBox() const
QAbstractButton *clickedButton() const
QPushButton *defaultButton() const
QString detailedText() const
QAbstractButton *escapeButton() const
QMessageBox::Icon icon() const
QPixmap iconPixmap() const
QString informativeText() const
void open(QObject *receiver, const char *member)
QMessageBox::Options options() const
void removeButton(QAbstractButton *button)
void setCheckBox(QCheckBox *cb)
void setDefaultButton(QMessageBox::StandardButton button)
void setDefaultButton(QPushButton *button)
void setDetailedText(const QString &text)
void setEscapeButton(QAbstractButton *button)
void setEscapeButton(QMessageBox::StandardButton button)
void setIcon(QMessageBox::Icon)
void setIconPixmap(const QPixmap &pixmap)
void setInformativeText(const QString &text)
(since 6.6) void setOption(QMessageBox::Option option, bool on = true)
void setOptions(QMessageBox::Options options)
void setStandardButtons(QMessageBox::StandardButtons buttons)
void setText(const QString &text)
void setTextFormat(Qt::TextFormat format)
void setTextInteractionFlags(Qt::TextInteractionFlags flags)
void setWindowModality(Qt::WindowModality windowModality)
void setWindowTitle(const QString &title)
QMessageBox::StandardButton standardButton(QAbstractButton *button) const
QMessageBox::StandardButtons standardButtons() const
(since 6.6) bool testOption(QMessageBox::Option option) const
QString text() const
Qt::TextFormat textFormat() const
Qt::TextInteractionFlags textInteractionFlags() const

공용 슬롯

virtual int exec() override

신호

void buttonClicked(QAbstractButton *button)

정적 공용 멤버

void about(QWidget *parent, const QString &title, const QString &text)
void aboutQt(QWidget *parent, const QString &title = QString())
QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

재구현된 보호 함수

virtual void changeEvent(QEvent *ev) override
virtual void closeEvent(QCloseEvent *e) override
virtual bool event(QEvent *e) override
virtual void keyPressEvent(QKeyEvent *e) override
virtual void resizeEvent(QResizeEvent *event) override
virtual void showEvent(QShowEvent *e) override

매크로

QT_REQUIRE_VERSION(int argc, char **argv, const char *version)

상세 설명

메시지 상자는 사용자에게 상황을 알리는 기본 text, 상황을 자세히 설명하는 informative text, 사용자가 요청하는 경우 추가 데이터를 제공하는 detailed text (선택 사항)를 표시합니다.

메시지 상자에 iconstandard buttons 을 표시하여 사용자 응답을 수락할 수도 있습니다.

QMessageBox를 사용하기 위한 두 가지 API, 즉 속성 기반 API와 정적 함수가 제공됩니다. 정적 함수 중 하나를 호출하는 것이 더 간단한 접근 방식이지만 속성 기반 API를 사용하는 것보다 유연성이 떨어지고 결과의 정보가 적습니다. 속성 기반 API를 사용하는 것이 좋습니다.

속성 기반 API

속성 기반 API를 사용하려면 QMessageBox의 인스턴스를 만들고 원하는 속성을 설정한 다음 exec()를 호출하여 메시지를 표시합니다. 가장 간단한 구성은 message text 속성만 설정하는 것입니다.

QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.exec();

사용자가 메시지 상자를 닫으려면 OK 버튼을 클릭해야 합니다. 메시지 상자가 해제될 때까지 나머지 GUI는 차단됩니다.

사용자에게 이벤트를 알리는 것보다 더 나은 접근 방식은 사용자에게 이벤트에 대해 무엇을 해야 하는지 묻는 것입니다.

standard buttons 속성을 사용자 응답 집합으로 원하는 버튼 집합으로 설정합니다. 버튼은 비트 OR 연산자를 사용하여 StandardButtons 의 값을 결합하여 지정합니다. 버튼의 표시 순서는 플랫폼에 따라 다릅니다. 예를 들어 Windows에서는 SaveCancel 의 왼쪽에 표시되는 반면, macOS에서는 순서가 반대로 표시됩니다. 표준 버튼 중 하나를 default button 으로 표시합니다.

informative text 속성을 사용하여 사용자가 적절한 작업을 선택하는 데 도움이 되는 추가 컨텍스트를 추가할 수 있습니다.

QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();

exec() 슬롯은 클릭한 버튼의 StandardButtons 값을 반환합니다.

switch (ret) {
  case QMessageBox::Save:
      // Save was clicked
      break;
  case QMessageBox::Discard:
      // Don't Save was clicked
      break;
  case QMessageBox::Cancel:
      // Cancel was clicked
      break;
  default:
      // should never be reached
      break;
}

사용자가 적절한 작업을 선택하는 데 도움이 되는 추가 정보를 제공하려면 detailed text 속성을 설정합니다. 플랫폼에 따라 detailed text, 표시하려면 사용자가 Show Details... 버튼을 클릭해야 할 수도 있습니다.

Show Details... 버튼을 클릭하면 자세한 텍스트가 표시됩니다.

서식 있는 텍스트 및 텍스트 형식 속성

detailed text 속성은 항상 일반 텍스트로 해석됩니다. main textinformative text 속성은 일반 텍스트 또는 서식 있는 텍스트일 수 있습니다. 이러한 문자열은 text format 속성의 설정에 따라 해석됩니다. 기본 설정은 auto-text 입니다.

XML 메타 문자가 포함된 일부 일반 텍스트 문자열의 경우 자동 텍스트 rich text detection test 가 실패하여 일반 텍스트 문자열이 서식 있는 텍스트로 잘못 해석될 수 있습니다. 이러한 드문 경우에는 Qt::convertFromPlainText()를 사용하여 일반 텍스트 문자열을 시각적으로 동등한 서식 있는 텍스트 문자열로 변환하거나 setTextFormat()를 사용하여 text format 속성을 명시적으로 설정하세요.

심각도 수준과 아이콘 및 픽스맵 속성

QMessageBox는 미리 정의된 네 가지 메시지 심각도 수준 또는 메시지 유형을 지원하며, 실제로는 각각 표시되는 미리 정의된 아이콘만 다릅니다. icon 속성을 predefined icons 중 하나로 설정하여 미리 정의된 네 가지 메시지 유형 중 하나를 지정합니다. 다음 규칙은 가이드라인입니다:

Question정상적인 작업 중 질문을 하는 경우.

Information정상 작동에 대한 정보 보고용.

Warning중요하지 않은 오류를 보고하는 경우.

Critical중대한 오류 보고용.

Predefined icons 는 QMessageBox에 의해 정의되지 않고 스타일에 의해 제공됩니다. 기본값은 No Icon 입니다. 그렇지 않으면 메시지 상자는 모든 경우에 대해 동일합니다. 표준 아이콘을 사용할 때는 표에서 권장하는 아이콘을 사용하거나 플랫폼의 스타일 가이드라인에서 권장하는 아이콘을 사용하세요. 메시지 상자에 적합한 표준 아이콘이 없는 경우에는 icon 속성을 설정하는 대신 icon pixmap 속성을 설정하여 사용자 지정 아이콘을 사용할 수 있습니다.

요약하면, 아이콘을 설정하려면 표준 아이콘 중 하나에 setIcon() 를 사용하거나 사용자 지정 아이콘에 setIconPixmap()를 사용합니다.

정적 함수 API

정적 함수 API로 메시지 상자를 작성하는 것은 편리하지만 정적 함수 서명에는 informative textdetailed text 속성을 설정하기 위한 매개 변수가 없기 때문에 속성 기반 API를 사용하는 것보다 유연성이 떨어집니다. 이에 대한 한 가지 해결 방법은 title 파라미터를 메시지 상자의 기본 텍스트로 사용하고 text 파라미터를 메시지 상자의 정보 텍스트로 사용하는 것이었습니다. 이 방법은 메시지 상자의 가독성을 떨어뜨린다는 명백한 단점이 있으므로 플랫폼 가이드라인에서는 이를 권장하지 않습니다. Microsoft Windows 사용자 인터페이스 가이드라인에서는 application namewindow's title 으로 사용할 것을 권장하므로, 기본 텍스트 외에 정보 텍스트가 있는 경우 text 매개변수에 연결해야 합니다.

정적 함수 서명은 버튼 매개변수와 관련하여 변경되었으며, 이제 standard buttonsdefault button 을 설정하는 데 사용됩니다.

정적 함수는 information(), question(), warning() 및 critical() 메시지 상자를 만드는 데 사용할 수 있습니다.

int ret = QMessageBox::warning(this, tr("My Application"),
                               tr("The document has been modified.\n"
                                  "Do you want to save your changes?"),
                               QMessageBox::Save | QMessageBox::Discard
                               | QMessageBox::Cancel,
                               QMessageBox::Save);

표준 대화상자 예제에서는 QMessageBox와 다른 내장 Qt 대화상자를 사용하는 방법을 보여줍니다.

고급 사용법

standard buttons 메시지 상자가 충분히 유연하지 않은 경우 텍스트와 ButtonRole 를 받는 addButton() 오버로드를 사용하여 사용자 지정 버튼을 추가할 수 있습니다. ButtonRole 은 QMessageBox에서 화면의 버튼 순서를 결정하는 데 사용됩니다(플랫폼에 따라 다름). exec ()을 호출한 후 clickedButton()의 값을 테스트할 수 있습니다. 예를 들어

QMessageBox msgBox;
QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);

msgBox.exec();

if (msgBox.clickedButton() == connectButton) {
    // connect
} else if (msgBox.clickedButton() == abortButton) {
    // abort
}

기본 및 이스케이프 키

기본 버튼(즉, Enter 을 눌렀을 때 활성화되는 버튼)은 setDefaultButton()을 사용하여 지정할 수 있습니다. 기본 버튼이 지정되지 않은 경우 QMessageBox는 메시지 상자에 사용된 버튼 중 button roles 을 기준으로 기본 버튼을 찾으려고 시도합니다.

이스케이프 버튼( Esc 을 눌렀을 때 활성화되는 버튼)은 setEscapeButton()을 사용하여 지정할 수 있습니다. 이스케이프 버튼이 지정되지 않은 경우 QMessageBox는 이 규칙을 사용하여 이스케이프 버튼을 찾으려고 시도합니다:

  1. 버튼이 하나만 있는 경우 Esc 을 눌렀을 때 활성화되는 버튼입니다.
  2. Cancel 버튼이 있는 경우 Esc 을 눌렀을 때 활성화되는 버튼입니다.
  3. the Reject role 또는 the No role 버튼이 정확히 하나만 있는 경우 Esc 을 눌렀을 때 활성화되는 버튼이 됩니다.

이러한 규칙을 사용하여 이스케이프 버튼을 결정할 수 없는 경우 Esc 을 눌러도 아무런 효과가 없습니다.

QDialogButtonBox표준 대화 상자 예시를참조하세요 .

회원 유형 문서

enum QMessageBox::ButtonRole

이 열거형은 버튼 상자의 버튼을 설명하는 데 사용할 수 있는 역할을 설명합니다. 이러한 역할의 조합은 동작의 다양한 측면을 설명하는 데 사용되는 플래그와 같습니다.

상수설명
QMessageBox::InvalidRole-1버튼이 유효하지 않습니다.
QMessageBox::AcceptRole0버튼을 클릭하면 대화 상자가 수락됩니다(예: 확인).
QMessageBox::RejectRole1버튼을 클릭하면 대화 상자가 거부됩니다(예: 취소).
QMessageBox::DestructiveRole2버튼을 클릭하면 파괴적인 변경(예: 변경 내용 삭제)이 발생하고 대화 상자가 닫힙니다.
QMessageBox::ActionRole3버튼을 클릭하면 대화 상자 내의 요소가 변경됩니다.
QMessageBox::HelpRole4버튼을 클릭하여 도움을 요청할 수 있습니다.
QMessageBox::YesRole5버튼은 "예"와 같은 버튼입니다.
QMessageBox::NoRole6버튼은 "아니요"와 같은 버튼입니다.
QMessageBox::ApplyRole8이 버튼은 현재 변경 사항을 적용합니다.
QMessageBox::ResetRole7이 버튼은 대화 상자의 필드를 기본값으로 재설정합니다.

StandardButton참조하세요 .

enum QMessageBox::Icon

이 열거형에는 다음과 같은 값이 있습니다:

Constant설명
QMessageBox::NoIcon0메시지 상자에 아이콘이 없습니다.
QMessageBox::Question4메시지가 질문을 하고 있음을 나타내는 아이콘입니다.
QMessageBox::Information1메시지가 평범하지 않은 메시지임을 나타내는 아이콘.
QMessageBox::Warning2메시지가 경고이지만 처리할 수 있음을 나타내는 아이콘입니다.
QMessageBox::Critical3메시지가 심각한 문제임을 나타내는 아이콘.

[since 6.6] 열거형 클래스 QMessageBox::Option
플래그 QMessageBox::옵션

Constant설명
QMessageBox::Option::DontUseNativeDialog0x00000001기본 메시지 대화 상자를 사용하지 않습니다.

이 열거형은 Qt 6.6에 도입되었습니다.

Options 유형은 QFlags<Option>에 대한 typedef입니다. 옵션 값의 OR 조합을 저장합니다.

열거형 QMessageBox::StandardButton
플래그 QMessageBox::StandardButtons

이 열거형은 표준 버튼에 대한 플래그를 설명합니다. 각 버튼에는 정의된 ButtonRole.

상수설명
QMessageBox::Ok0x00000400AcceptRole 로 정의된 "확인" 버튼입니다.
QMessageBox::Open0x00002000와 함께 정의된 "열기" 버튼 AcceptRole.
QMessageBox::Save0x00000800를 사용하여 정의된 "저장" 버튼 AcceptRole.
QMessageBox::Cancel0x00400000RejectRole 로 정의된 "취소" 버튼.
QMessageBox::Close0x00200000RejectRole 로 정의된 "닫기" 버튼.
QMessageBox::Discard0x00800000플랫폼에 따라 DestructiveRole 로 정의된 "삭제" 또는 "저장 안 함" 버튼.
QMessageBox::Apply0x02000000ApplyRole 로 정의된 "적용" 버튼.
QMessageBox::Reset0x04000000ResetRole 로 정의된 "재설정" 버튼.
QMessageBox::RestoreDefaults0x08000000ResetRole 로 정의된 "기본값 복원" 버튼.
QMessageBox::Help0x01000000HelpRole 로 정의된 "도움말" 버튼.
QMessageBox::SaveAll0x00001000AcceptRole 로 정의된 "모두 저장" 버튼.
QMessageBox::Yes0x00004000YesRole 로 정의된 "예" 버튼.
QMessageBox::YesToAll0x00008000와 함께 정의된 "예, 모두" 버튼 YesRole.
QMessageBox::No0x00010000와 함께 정의된 "아니요" 버튼 NoRole.
QMessageBox::NoToAll0x00020000와 함께 정의된 "아니요 - 모두" 버튼 NoRole.
QMessageBox::Abort0x00040000와 함께 정의된 "중단" 버튼 RejectRole.
QMessageBox::Retry0x00080000AcceptRole 로 정의된 "재시도" 버튼.
QMessageBox::Ignore0x00100000와 함께 정의된 "무시" 버튼 AcceptRole.
QMessageBox::NoButton0x00000000유효하지 않은 버튼입니다.

다음 값은 더 이상 사용되지 않습니다:

상수설명
QMessageBox::YesAllYesToAll대신 YesToAll을 사용합니다.
QMessageBox::NoAllNoToAll대신 NoToAll을 사용합니다.
QMessageBox::Default0x00000100information(), warning() 등의 defaultButton 인수를 대신 사용하거나 setDefaultButton()를 호출합니다.
QMessageBox::Escape0x00000200setEscapeButton()를 대신 호출합니다.
QMessageBox::FlagMask0x00000300
QMessageBox::ButtonMask~FlagMask

StandardButtons 유형은 QFlags<StandardButton에 대한 typedef입니다. StandardButton 값의 OR 조합을 저장합니다.

ButtonRolestandardButtons참조하십시오 .

속성 문서

detailedText : QString

이 속성은 세부 정보 영역에 표시할 텍스트를 저장합니다.

텍스트는 일반 텍스트로 해석됩니다.

기본적으로 이 속성에는 빈 문자열이 포함됩니다.

함수에 액세스합니다:

QString detailedText() const
void setDetailedText(const QString &text)

QMessageBox::textQMessageBox::informativeText참조하세요 .

icon : Icon

이 속성은 메시지 상자의 아이콘을 보유합니다.

메시지 상자의 아이콘은 값 중 하나를 사용하여 지정할 수 있습니다:

기본값은 QMessageBox::NoIcon 입니다.

실제 아이콘을 표시하는 데 사용되는 픽셀맵은 현재 GUI style 에 따라 달라집니다. icon pixmap 속성을 설정하여 아이콘에 대한 사용자 지정 픽스맵을 설정할 수도 있습니다.

기능에 액세스합니다:

QMessageBox::Icon icon() const
void setIcon(QMessageBox::Icon)

iconPixmap참조하세요 .

iconPixmap : QPixmap

이 속성은 현재 아이콘을 보유합니다.

메시지 상자에 현재 사용되는 아이콘입니다. 모든 GUI 스타일에 적합한 하나의 픽셀맵을 그리는 것은 어려운 경우가 많으므로 각 플랫폼마다 다른 픽셀맵을 제공해야 할 수도 있습니다.

기본적으로 이 속성은 정의되지 않습니다.

함수 액세스:

QPixmap iconPixmap() const
void setIconPixmap(const QPixmap &pixmap)

icon참조하세요 .

informativeText : QString

이 속성에는 메시지에 대한 자세한 설명을 제공하는 정보성 텍스트가 포함됩니다.

정보 제공 텍스트는 text()를 확장하여 사용자에게 상황의 결과를 설명하거나 대체 솔루션을 제안하는 등 더 많은 정보를 제공하는 데 사용할 수 있습니다.

텍스트는 텍스트 형식 설정(QMessageBox::textFormat)에 따라 일반 텍스트 또는 서식 있는 텍스트로 해석됩니다. 기본 설정은 Qt::AutoText 이며, 메시지 상자가 텍스트 형식을 자동으로 감지하려고 시도합니다.

기본적으로 이 속성에는 빈 문자열이 포함됩니다.

함수 액세스:

QString informativeText() const
void setInformativeText(const QString &text)

textFormat, QMessageBox::text, QMessageBox::detailedText참조하세요 .

[since 6.6] options : Options

대화 상자의 모양과 느낌에 영향을 주는 옵션입니다.

기본적으로 이러한 옵션은 비활성화되어 있습니다.

대화 상자 속성을 변경하거나 대화 상자를 표시하기 전에 Option::DontUseNativeDialog 옵션을 설정해야 합니다.

대화 상자가 표시된 상태에서 옵션을 설정해도 대화 상자에 즉각적인 영향을 미치지는 않습니다.

다른 속성을 변경한 후에 옵션을 설정하면 해당 값이 적용되지 않을 수 있습니다.

이 속성은 Qt 6.6에 도입되었습니다.

액세스 함수:

QMessageBox::Options options() const
void setOptions(QMessageBox::Options options)

setOption() 및 testOption()도 참조하세요 .

standardButtons : StandardButtons

메시지 상자의 표준 버튼 모음

이 속성은 메시지 상자에서 사용할 표준 버튼을 제어합니다.

기본적으로 이 속성에는 표준 버튼이 포함되어 있지 않습니다.

함수에 액세스합니다:

QMessageBox::StandardButtons standardButtons() const
void setStandardButtons(QMessageBox::StandardButtons buttons)

addButton()도 참조하세요 .

text : QString

이 속성에는 표시할 메시지 상자 텍스트가 저장됩니다.

텍스트는 상황을 설명하는 간단한 문장이나 구문이어야 하며, 중립적인 문장으로 작성하거나 클릭 유도 문항으로 구성하는 것이 가장 이상적입니다.

텍스트는 텍스트 형식 설정(QMessageBox::textFormat)에 따라 일반 텍스트 또는 서식 있는 텍스트로 해석됩니다. 기본 설정은 Qt::AutoText 즉, 메시지 상자에서 텍스트 형식을 자동으로 감지합니다.

이 속성의 기본값은 빈 문자열입니다.

함수 액세스:

QString text() const
void setText(const QString &text)

textFormat, QMessageBox::informativeText, QMessageBox::detailedText참조하세요 .

textFormat : Qt::TextFormat

이 속성은 메시지 상자에 표시되는 텍스트의 형식을 저장합니다.

메시지 상자에 사용되는 현재 텍스트 형식입니다. 사용 가능한 옵션에 대한 설명은 Qt::TextFormat 열거 형을 참조하세요.

기본 형식은 Qt::AutoText 입니다.

함수 액세스:

Qt::TextFormat textFormat() const
void setTextFormat(Qt::TextFormat format)

setText()도 참조하세요 .

textInteractionFlags : Qt::TextInteractionFlags

메시지 상자의 레이블이 사용자 입력과 상호 작용하는 방식을 지정합니다.

기본값은 스타일에 따라 다릅니다.

액세스 함수:

Qt::TextInteractionFlags textInteractionFlags() const
void setTextInteractionFlags(Qt::TextInteractionFlags flags)

QStyle::SH_MessageBox_TextInteractionFlags참조하세요 .

멤버 함수 문서

[explicit] QMessageBox::QMessageBox(QWidget *parent = nullptr)

텍스트와 버튼이 없는 application modal 메시지 상자를 만듭니다. parentQDialog 생성자에게 전달됩니다.

창 양식은 show()을 호출하기 전에 setWindowModality()를 통해 재정의할 수 있습니다.

참고: open() 또는 exec()를 사용하여 메시지 상자를 표시하면 창 양식에 영향을 줍니다. 자세한 내용은 각 기능에 대한 자세한 설명서를 참조하세요.

macOS에서 메시지 상자를 parentQt::Sheet 로 표시하려면 메시지 상자의 window modalityQt::WindowModal 으로 설정하거나 open()를 사용합니다. 그렇지 않으면 메시지 상자가 표준 대화 상자가 됩니다.

setWindowTitle(), setText(), setIcon(), setStandardButtons() 및 setWindowModality()도 참조하세요 .

QMessageBox::QMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)

주어진 icon, title, text, 표준 buttons 으로 application modal 메시지 상자를 만듭니다. 표준 또는 사용자 정의 버튼은 addButton()를 사용하여 언제든지 추가할 수 있습니다. parentf 인수는 QDialog 생성자에게 전달됩니다.

창 양식은 show()을 호출하기 전에 setWindowModality()를 통해 재정의할 수 있습니다.

참고: open() 또는 exec()를 사용하여 메시지 상자를 표시하면 창 양식에 영향을 줍니다. 자세한 내용은 각 기능에 대한 자세한 설명서를 참조하세요.

parent macOS에서 nullptr 이 아닌 Qt::Sheet 으로 메시지 상자를 표시하려는 경우 메시지 상자의 window modalityQt::WindowModal (기본값)으로 설정합니다. 그렇지 않으면 메시지 상자가 표준 대화 상자가 됩니다.

setWindowTitle(), setText(), setIcon(), setStandardButtons() 및 setWindowModality()도 참조하세요 .

[virtual noexcept] QMessageBox::~QMessageBox()

메시지 상자를 삭제합니다.

[static] void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)

제목 title 과 텍스트 text 로 간단한 정보 상자를 표시합니다. 정보 상자의 부모는 parent 입니다.

about()은 네 위치에서 적합한 아이콘을 찾습니다:

  1. parent->icon() 아이콘이 있으면 이를 선호합니다.
  2. 없는 경우 parent 이 포함된 최상위 위젯을 시도합니다.
  3. 그래도 실패하면 active window.
  4. 최후의 수단으로 정보 아이콘을 사용합니다.

정보 상자에는 "확인"이라는 레이블이 붙은 버튼이 하나 있습니다.

macOS에서는 정보 상자가 모델 없는 창으로 팝업되지만 다른 플랫폼에서는 현재 애플리케이션 모달로 표시됩니다.

QWidget::windowIcon() 및 QApplication::activeWindow()도 참조하세요 .

[static] void QMessageBox::aboutQt(QWidget *parent, const QString &title = QString())

지정된 title 및 중앙에 parent ( parentnullptr 이 아닌 경우)이 있는 Qt에 대한 간단한 메시지 상자를 표시합니다. 이 메시지에는 애플리케이션에서 사용 중인 Qt의 버전 번호가 포함됩니다.

이것은 메뉴 예제와 같이 애플리케이션의 Help 메뉴에 포함시킬 때 유용합니다.

QApplication 는 이 기능을 슬롯으로 제공합니다.

macOS에서는 aboutQt 상자가 모델 없는 창으로 팝업되지만, 다른 플랫폼에서는 현재 애플리케이션 모달로 표시됩니다.

QApplication::aboutQt()도 참조하십시오 .

void QMessageBox::addButton(QAbstractButton *button, QMessageBox::ButtonRole role)

지정된 button 을 지정된 role 과 함께 메시지 상자에 추가합니다.

removeButton(), button() 및 setStandardButtons()도 참조하세요 .

QPushButton *QMessageBox::addButton(QMessageBox::StandardButton button)

이 함수는 오버로드된 함수입니다.

메시지 상자에 표준 button 을 추가하는 것이 유효한 경우 메시지 상자에 추가하고 푸시 버튼을 반환합니다.

setStandardButtons()도 참조하세요 .

QPushButton *QMessageBox::addButton(const QString &text, QMessageBox::ButtonRole role)

이 함수는 오버로드된 함수입니다.

지정된 text 으로 버튼을 생성하고 지정된 role 에 대한 메시지 상자에 추가한 다음 반환합니다.

QAbstractButton *QMessageBox::button(QMessageBox::StandardButton which) const

표준 버튼에 해당하는 포인터 which, 또는 이 메시지 상자에 표준 버튼이 없는 경우 nullptr 을 반환합니다.

참고: 반환된 버튼의 속성을 수정하면 메시지 대화 상자의 기본 구현에 반영되지 않을 수 있습니다. 대화 상자 버튼을 사용자 지정하려면 custom button 또는 button title 을 대신 추가하거나 Option::DontUseNativeDialog 옵션을 설정하세요.

standardButtonsstandardButton()도 참조하세요 .

[signal] void QMessageBox::buttonClicked(QAbstractButton *button)

이 신호는 QMessageBox 에서 버튼을 클릭할 때마다 발생하며, 클릭한 버튼은 button 으로 반환됩니다.

QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button) const

지정된 button 에 대한 버튼 역할을 반환합니다. buttonnullptr 이거나 메시지 상자에 추가되지 않은 경우 이 함수는 InvalidRole 을 반환합니다.

buttons() 및 addButton()도 참조하세요 .

QList<QAbstractButton *> QMessageBox::buttons() const

메시지 상자에 추가된 모든 버튼의 목록을 반환합니다.

buttonRole(), addButton() 및 removeButton()도 참조하세요 .

[override virtual protected] void QMessageBox::changeEvent(QEvent *ev)

다시 구현합니다: QWidget::changeEvent(QEvent * 이벤트).

QCheckBox *QMessageBox::checkBox() const

대화 상자에 표시된 확인란을 반환합니다. 확인란이 설정되지 않은 경우 이 값은 nullptr 입니다.

setCheckBox()도 참조하세요 .

QAbstractButton *QMessageBox::clickedButton() const

사용자가 클릭한 버튼을 반환하거나, 사용자가 Esc 키를 누르고 escape button 이 설정되지 않은 경우 nullptr 을 반환합니다.

exec()가 아직 호출되지 않은 경우 nullptr을 반환합니다.

예시:

QMessageBox messageBox(this);
QAbstractButton *disconnectButton =
      messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole);
...
messageBox.exec();
if (messageBox.clickedButton() == disconnectButton) {
    ...
}

standardButton() 및 button()도 참조하세요 .

[override virtual protected] void QMessageBox::closeEvent(QCloseEvent *e)

다시 구현합니다: QDialog::closeEvent(QCloseEvent *e).

[static] QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

지정된 parent 위젯 앞에 지정된 titletext 이 있는 중요 메시지 상자를 엽니다.

표준 buttons 이 메시지 상자에 추가됩니다. defaultButtonEnter 을 누를 때 사용되는 버튼을 지정합니다. defaultButtonbuttons 에 지정된 버튼을 참조해야 합니다. defaultButtonQMessageBox::NoButton 인 경우 QMessageBox 은 적절한 기본값을 자동으로 선택합니다.

클릭한 표준 버튼의 ID를 반환합니다. Esc 을 대신 눌렀으면 escape button 이 반환됩니다.

메시지 상자는 application modal 대화 상자입니다.

경고: 대화 상자가 실행되는 동안 parent 을 삭제하지 마세요. 이 작업을 수행하려면 QMessageBox 생성자 중 하나를 사용하여 대화 상자를 직접 만들어야 합니다.

question(), warning() 및 information()도 참조하세요 .

QPushButton *QMessageBox::defaultButton() const

메시지 상자의 default button 버튼이 될 버튼을 반환합니다. 기본 버튼이 설정되지 않은 경우 nullptr을 반환합니다.

setDefaultButton(), addButton() 및 QPushButton::setDefault()도 참조하세요 .

QAbstractButton *QMessageBox::escapeButton() const

이스케이프 버튼을 눌렀을 때 활성화된 버튼을 반환합니다.

기본적으로 QMessageBox 은 다음과 같이 이스케이프 버튼을 자동으로 감지하려고 시도합니다:

  1. 버튼이 하나만 있는 경우 해당 버튼을 이스케이프 버튼으로 만듭니다.
  2. Cancel 버튼이 있는 경우 해당 버튼이 이스케이프 버튼이 됩니다.
  3. macOS에서만 역할이 QMessageBox::RejectRole 인 버튼이 정확히 하나만 있으면 이 버튼이 이스케이프 버튼으로 만들어집니다.

이스케이프 버튼을 자동으로 감지할 수 없는 경우 Esc 을 눌러도 아무런 효과가 없습니다.

setEscapeButton() 및 addButton()도 참조하세요 .

[override virtual protected] bool QMessageBox::event(QEvent *e)

다시 구현합니다: QWidget::event(QEvent * 이벤트).

[override virtual slot] int QMessageBox::exec()

다시 구현합니다: QDialog::exec().

메시지 상자를 modal dialog 로 표시하여 사용자가 닫을 때까지 차단합니다.

표준 버튼과 함께 QMessageBox 을 사용하는 경우 이 함수는 클릭된 표준 버튼을 나타내는 StandardButton 값을 반환합니다. 사용자 지정 버튼과 함께 QMessageBox 을 사용하는 경우 이 함수는 불투명한 값을 반환하며, clickedButton()을 사용하여 어떤 버튼이 클릭되었는지 확인합니다.

참고: result() 함수는 QDialog::DialogCode 대신 StandardButton 값도 반환합니다.

사용자는 버튼을 클릭하거나 창 시스템에서 제공하는 메커니즘을 사용하여 대화 상자를 닫을 때까지 동일한 애플리케이션의 다른 창과 상호 작용할 수 없습니다.

show() 및 result()도 참조하세요 .

[static] QMessageBox::StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

지정된 parent 위젯 앞에 지정된 titletext 이 있는 정보 메시지 상자를 엽니다.

표준 buttons 이 메시지 상자에 추가됩니다. defaultButtonEnter 을 누를 때 사용되는 버튼을 지정합니다. defaultButtonbuttons 에 지정된 버튼을 참조해야 합니다. defaultButtonQMessageBox::NoButton 인 경우 QMessageBox 은 적절한 기본값을 자동으로 선택합니다.

클릭한 표준 버튼의 ID를 반환합니다. Esc 을 대신 눌렀으면 escape button 이 반환됩니다.

메시지 상자는 application modal 대화 상자입니다.

경고: 대화 상자가 실행되는 동안 parent 을 삭제하지 마세요. 이 작업을 수행하려면 QMessageBox 생성자 중 하나를 사용하여 대화 상자를 직접 만들어야 합니다.

question(), warning() 및 critical()도 참조하세요 .

[override virtual protected] void QMessageBox::keyPressEvent(QKeyEvent *e)

다시 구현합니다: QDialog::keyPressEvent(QKeyEvent *e).

void QMessageBox::open(QObject *receiver, const char *member)

대화 상자를 열고 finished() 또는 buttonClicked() 신호를 receivermember 에 지정된 슬롯에 연결합니다. member 의 슬롯에 첫 번째 매개변수에 대한 포인터가 있으면 buttonClicked()에 연결되고, 그렇지 않으면 finished()에 연결됩니다.

대화 상자가 닫히면 슬롯에서 신호 연결이 끊어집니다.

[static] QMessageBox::StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)

지정된 parent 위젯 앞에 지정된 titletext 이 있는 질문 메시지 상자를 엽니다.

표준 buttons 이 메시지 상자에 추가됩니다. defaultButtonEnter 을 누를 때 사용되는 버튼을 지정합니다. defaultButtonbuttons 에 지정된 버튼을 참조해야 합니다. defaultButtonQMessageBox::NoButton 인 경우 QMessageBox 은 적절한 기본값을 자동으로 선택합니다.

클릭한 표준 버튼의 ID를 반환합니다. Esc 을 대신 눌렀으면 escape button 이 반환됩니다.

메시지 상자는 application modal 대화 상자입니다.

경고: 대화 상자가 실행되는 동안 parent 을 삭제하지 마세요. 이 작업을 수행하려면 QMessageBox 생성자 중 하나를 사용하여 대화 상자를 직접 만들어야 합니다.

information(), warning() 및 critical()도 참조하세요 .

void QMessageBox::removeButton(QAbstractButton *button)

버튼 상자에서 button 을 삭제하지 않고 제거합니다.

addButton() 및 setStandardButtons()도 참조하세요 .

[override virtual protected] void QMessageBox::resizeEvent(QResizeEvent *event)

다시 구현합니다: QDialog::resizeEvent(QResizeEvent *).

void QMessageBox::setCheckBox(QCheckBox *cb)

메시지 대화 상자에서 cb 확인란을 설정합니다. 메시지 상자가 확인란의 소유권을 갖습니다. cb 인수는 nullptr 으로 지정하여 메시지 상자에서 기존 확인란을 제거할 수 있습니다.

checkBox()도 참조하세요 .

void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)

메시지 상자의 default buttonbutton 으로 설정합니다.

defaultButton(), addButton() 및 QPushButton::setDefault()도 참조하세요 .

void QMessageBox::setDefaultButton(QPushButton *button)

메시지 상자의 default buttonbutton 으로 설정합니다.

addButton() 및 QPushButton::setDefault()도 참조하세요 .

void QMessageBox::setEscapeButton(QAbstractButton *button)

Escape 키를 누를 때 활성화되는 버튼을 button 으로 설정합니다.

escapeButton(), addButton() 및 clickedButton()도 참조하세요 .

void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)

Escape 키를 누를 때 활성화되는 버튼을 button 으로 설정합니다.

addButton() 및 clickedButton()도 참조하세요 .

[since 6.6] void QMessageBox::setOption(QMessageBox::Option option, bool on = true)

on 이 참이면 지정된 option 을 활성화하도록 설정하고, 그렇지 않으면 지정된 option 을 지웁니다.

옵션(특히 Option::DontUseNativeDialog 옵션)은 대화 상자를 표시하기 전에 설정해야 합니다.

대화 상자가 표시된 상태에서 옵션을 설정하면 대화 상자에 즉각적인 영향을 미치지 않습니다.

다른 속성을 변경한 후에 옵션을 설정하면 해당 값이 적용되지 않을 수 있습니다.

이 기능은 Qt 6.6에 도입되었습니다.

optionstestOption()도 참조하세요 .

void QMessageBox::setWindowModality(Qt::WindowModality windowModality)

이 함수는 QWidget::setWindowModality()를 섀도잉합니다.

메시지 상자의 모달리티를 windowModality 로 설정합니다.

macOS에서 양식이 Qt::WindowModal 로 설정되어 있고 메시지 상자에 부모가 있는 경우 메시지 상자는 Qt::Sheet 이 되고 그렇지 않은 경우 메시지 상자는 표준 대화 상자가 됩니다.

void QMessageBox::setWindowTitle(const QString &title)

이 함수는 QWidget::setWindowTitle()를 그림자로 표시합니다.

메시지 상자의 제목을 title 로 설정합니다. macOS에서는 창 제목이 무시됩니다(macOS 가이드라인에 따라 요구됨).

[override virtual protected] void QMessageBox::showEvent(QShowEvent *e)

다시 구현합니다: QDialog::showEvent(QShowEvent * 이벤트).

QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button) const

주어진 button 에 해당하는 표준 버튼 열거형 값을 반환하거나, 주어진 button 이 표준 버튼이 아닌 경우 NoButton 을 반환합니다.

button() 및 standardButtons()도 참조하세요 .

[since 6.6] bool QMessageBox::testOption(QMessageBox::Option option) const

주어진 option 이 활성화되면 true 을 반환하고, 그렇지 않으면 false를 반환합니다.

이 함수는 Qt 6.6에 도입되었습니다.

optionssetOption()도 참조하십시오 .

[static] QMessageBox::StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

지정된 parent 위젯 앞에 지정된 titletext 이 있는 경고 메시지 상자를 엽니다.

표준 buttons 이 메시지 상자에 추가됩니다. defaultButtonEnter 을 누를 때 사용되는 버튼을 지정합니다. defaultButtonbuttons 에 지정된 버튼을 참조해야 합니다. defaultButtonQMessageBox::NoButton 인 경우 QMessageBox 은 적절한 기본값을 자동으로 선택합니다.

클릭한 표준 버튼의 ID를 반환합니다. Esc 을 대신 눌렀으면 escape button 이 반환됩니다.

메시지 상자는 application modal 대화 상자입니다.

경고: 대화 상자가 실행되는 동안 parent 을 삭제하지 마세요. 이 작업을 수행하려면 QMessageBox 생성자 중 하나를 사용하여 대화 상자를 직접 만들어야 합니다.

question(), information() 및 critical()도 참조하세요 .

매크로 문서

QT_REQUIRE_VERSION(int argc, char **argv, const char *version)

이 매크로는 애플리케이션이 충분히 최신 버전의 Qt로 실행되도록 하는 데 사용할 수 있습니다. 애플리케이션이 버그 수정 릴리스(예: 6.1.2)에서 도입된 특정 버그 수정에 의존하는 경우 특히 유용합니다.

argcargv 매개 변수는 main() 함수의 argcargv 매개 변수입니다. version 매개변수는 애플리케이션에 필요한 Qt 버전을 지정하는 문자열 리터럴입니다(예: "6.1.2").

예시:

#include <QApplication>
#include <QMessageBox>

int main(int argc, char *argv[])
{
    QT_REQUIRE_VERSION(argc, argv, "4.0.2")

    QApplication app(argc, argv);
    ...
    return app.exec();
}

© 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.