티스토리 뷰

C | C++

QMessageBox 클래스(QT C++)

야라바 2024. 2. 9. 09:42
728x90

간편하게 대화창으로 경고 메시지를 출력하거나, 간단한 확인 과정을 위한 대화창을 출력할 때 편리하게 사용할 수 있는 클래스로 QDialog를 상속받은 클래스이다. 두 가지 방식으로 사용할 수 있는데 한 가지 방식은 QMessageBox 클래스의 정적 함수(about(), critical(), information(), question(), warning())를 사용하는 것으로 용도에 따라 미리 준비되어 있는 것이므로 간편하게 사용할 수 있다는 장점이 있지만 유연성이 떨어진다. 또 다른 방식은 클래스의 여러 속성을 통하는 방법으로 조금 번거롭다고 느껴질 수 있지만 정적 함수를 사용하는 방법보다 효과적인 작업을 할 수 있다는 장점이 있다.

 

참조 링크 : https://doc.qt.io/qt-5/qmessagebox.html

 

int ret = QMessageBox::information(this, _("Saving the changes"),
    _("Some changes were made!\nSave the changes to the DB before exiting?"),
    QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes);
switch (ret) {
case QMessageBox::Yes:
  req.clear()->setName("save")->setAttr("path", "/" + SYS->id() + "/%2fobj");
  cntrIfCmd(req);
  return true;
case QMessageBox::No:
  return true;
case QMessageBox::Cancel:
  return false;
}

위의 예제는 information() 정적 함수를 사용하고 있는 것으로 현재창(this) 위에 지정한 제목과 내용, 그리고 버튼 조합으로  대화창을 출력하고 사용자가 버튼을 누르면 어떤 버튼을 누른 것인지를 리턴한다. 리턴, 표시할 버튼 조합, 기본 버튼에 사용하는 값은 다음과 같은 것들을 사용할 수 있다.

Ok, Open, Save, Cancel, Close, Discard, Apply, Reset, RestoreDefaults, Help, SaveAll, Yes, YesToAll, No, NoToAll, Abort, Retry, Ignore

 

QMessageBox::question(this, _("Moving or copying the visual items"),
  QString(_("The target item '%1' is already present!\nPaste to it anyway?")).arg(dlg.id()),
  QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) continue;

또 다른 정적 함수를 사용하고 있는 예제로 critical(), information(), question(), warning() 함수 모두 아이콘만 자동으로 바뀌고 아규먼트와 리턴 타입은 아래의 information() 프로토타입과 대동소이하다. 

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

"버튼 리턴 QMessageBox::information(부모위젯, 제목, 내용, 버튼 조합, 기본 버튼);"의 형태로 대화창에 엔터키를 누르면 기본 버튼으로 지정한 버튼을 누른 것으로 처리하는데, 기본 버튼을 지정하지 않으면 NoButton이 기본값이다. ESC 키를 누르면 Cancel 버튼을 누른 것으로 처리하는데 버튼이 하나만 있다면 ESC 키를 눌러도 해당 버튼을 누른 것으로 처리한다.

 

QMessageBox::about(this, windowTitle(), buf);

정적 함수 중에서 다른 형태를 취하는 것이 about()으로 별도의 대화창을 구성하지 않고도 간단하게 프로그램 정보를 표시할 수 있다. 아이콘은 부모창의 아이콘을 우선해서 사용한다.

 

QMessageBox msgBox(parent);
msgBox.setWindowTitle(_(MOD_NAME));
msgBox.setTextFormat(Qt::PlainText);
msgBox.setText(mess.c_str());
switch (type) {
case TUIMod::Info:
  msgBox.setIcon(QMessageBox::Information);
  break;
case TUIMod::Warning:
  msgBox.setIcon(QMessageBox::Warning);
  break;
case TUIMod::Error:
case TUIMod::Crit:
  msgBox.setIcon(QMessageBox::Critical);
  break;
}
msgBox.exec();

 

위의 코드는 속성을 개별적으로 지정하는 방식으로 조금 번거로워 보일 수 있지만 유연성 있게 대화창을 구성할 수 있는 방법이다. 주요 메서드는 다음과 같다.

 

void setWindowTitle(const QString &title) : 제목 설정

void setTextFormat(Qt::TextFormat format) : 내용 형식 지정.
    Qt::PlainText, Qt::RichText, Qt::AutoText, Qt::MarkdownText

void setText(const QString &text) : 내용 설정

void setIcon(QMessageBox::Icon) : 아이콘 설정. 기본값은 NoIcon
    QMessageBox::NoIcon, ::Question, ::Information, ::Warning, ::Critical

void setIconPixmap(const QPixmap &pixmap) : 사용자 아이콘 설정

QPushButton *addButton(const QString &text, QMessageBox::ButtonRole role) : 사용자 버튼 추가

QPushButton *addButton(QMessageBox::StandardButton button) : 표준 버튼 추가

QAbstractButton *clickedButton() : 어떤 버튼이 눌러졌는지 리턴. exec()를 수행하지 않았거나 ESC를 누르면 NULL

int exec() : 대화창 출력 후 대기.
    표준 버튼을 사용하면 어떤 버튼을 눌렀는지를 리턴 하지만, 아니면 clickedButton()로 확인해야 함.

void setDefaultButton(QPushButton *button) : 사용자 버튼으로 기본 버튼 설정

void setDefaultButton(QMessageBox::StandardButton button) : 표준버튼으로 기본 버튼 설정

void setEscapeButton(QAbstractButton *button) : 사용자 버튼으로 ESC 버튼 설정

void setEscapeButton(QMessageBox::StandardButton button) : 표준버튼으로 ESC 버튼 설정

 

 

728x90