티스토리 뷰

Qt/Classes

QMessageBox Code Examples

아침엔커피한잔 2017. 5. 19. 06:22

http://doc.qt.io/qt-5/QMessageBox.html



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

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

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


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

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

    QMessageBox::StandardButton btn = QMessageBox::question(this, tr("Reset autoincrement"), tr("Are you sure you want to reset autoincrement value for table '%1'?")
                                                            .arg(table));
    if (btn != QMessageBox::Yes)
        return;

void MainWindow::loadFile(const QString &fileName)
  {
      QFile file(fileName);
      if (!file.open(QFile::ReadOnly | QFile::Text)) {
          QMessageBox::warning(this, tr("Application"),
                               tr("Cannot read file %1:\n%2.")
                               .arg(QDir::toNativeSeparators(fileName), file.errorString()));
          return;
      }

      QTextStream in(&file);
  #ifndef QT_NO_CURSOR
      QApplication::setOverrideCursor(Qt::WaitCursor);
  #endif
      textEdit->setPlainText(in.readAll());
  #ifndef QT_NO_CURSOR
      QApplication::restoreOverrideCursor();
  #endif

      setCurrentFile(fileName);
      statusBar()->showMessage(tr("File loaded"), 2000);
  }

bool MainWindow::saveFile(const QString &fileName)
  {
      QFile file(fileName);
      if (!file.open(QFile::WriteOnly | QFile::Text)) {
          QMessageBox::warning(this, tr("Application"),
                               tr("Cannot write file %1:\n%2.")
                               .arg(QDir::toNativeSeparators(fileName),
                                    file.errorString()));
          return false;
      }

      QTextStream out(&file);
  #ifndef QT_NO_CURSOR
      QApplication::setOverrideCursor(Qt::WaitCursor);
  #endif
      out << textEdit->toPlainText();
  #ifndef QT_NO_CURSOR
      QApplication::restoreOverrideCursor();
  #endif

      setCurrentFile(fileName);
      statusBar()->showMessage(tr("File saved"), 2000);
      return true;
  }


'Qt > Classes' 카테고리의 다른 글

QAction Code Examples  (0) 2017.05.21
QSqlTableModel Code Examples  (0) 2017.05.19
QSettings Code Examples  (0) 2017.05.07
QTreeView QTreeWidget Code Examples  (0) 2017.05.02
QFontMetrics Code Examples  (0) 2017.05.02
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함