3

So I am making a program in Qt 5.2.1 . What I need is, when the user clicks a button, a warning appears, if the user clicks 'Ok' then program continues and if he clicks 'Cancel' nothing happens.

How do I do this? I'm a total newbie to Qt.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
user121273
  • 59
  • 1
  • 2
  • 9
  • 2
    refer to the documentation, there are many examples and (video) tutorials around to get you started (http://qt-project.org/doc/qt-5.0/qtdoc/qtexamplesandtutorials.html) – c_k Mar 13 '14 at 11:55
  • 1
    See [`QMessageBox`](http://qt-project.org/doc/qt-5.0/qtwidgets/qmessagebox.html). – thuga Mar 13 '14 at 12:15
  • Really, for Qt there are so many easy examples. Study them! – dgrat Mar 13 '14 at 12:16
  • @thuga I read that and came across qdebug and qwarning but the thing it does not answer my question. I want to check if the user clicks okay or not. – user121273 Mar 13 '14 at 13:06
  • 1
    You may wish to re-read the [`QMessageBox`](http://qt-project.org/doc/qt-5/qmessagebox.html) documentation if you think you "came across qdebug and qwarning" there. – Kuba hasn't forgotten Monica Mar 13 '14 at 14:33

1 Answers1

3

This is one sscce way to do it. I've strived to make it as correct and minimal as possible. I've paid attention to the following:

  1. Blocking methods that reenter the event loop are necessarily a source of bugs and should never be used. Thus we don't use QMessageBox::exec().

  2. Standard buttons are used.

  3. Both text and informative text are provided to conform with human interface guidelines across platforms.

  4. The message box's modality is set according to our real requirements. It is window-modal, preventing interaction with the underlying window, but not with the rest of the application.

  5. Child widgets are regular members and are not directly allocated on the heap. This makes memory management much easier and leverages RAII. Internally, they will heap-allocate their PIMPLs anyway.

  6. Default member initializers are used when constructing members.

  7. Widgets that are added to layouts are not passed a parent. Doing so would be redundant.

  8. Slots are given descriptive names that indicate both what widget and what signal they act upon. In conjunction with giving widgets object names, this lets us leverage the connectSlotsByName mechanism. It also eases the debugging, since the debugging helpers let us see object names when debugging a Qt application.

P.S. QDrag, I'm giving you the stare. You know, that stare.

// main.cpp
#include <QtGlobal>
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#include <QtGui>
#else
#include <QtWidgets>
#endif

class MyUi : public QWidget {
   Q_OBJECT
   QBoxLayout m_layout{QBoxLayout::TopToBottom, this};
   QLabel m_label;
   QPushButton m_button{"Change Message"};
   QMessageBox m_warning{QMessageBox::Warning, "Message Change",
                         "The message will change.",
                         QMessageBox::Yes | QMessageBox::No,
                         this},
   Q_SLOT void on_button_clicked() {
      m_warning.show();
   }
   Q_SLOT void on_warning_finished(int rc) {
      // The `finished()` signal is emitted with a
      // QDialogButtonBox::StandardButton value - the same that would
      // be retuned by QMessageBox::exec().
      // A QMessageBox does *not*  accept the dialog,
      // so we can't simply use the `accepted` signal.
      if (rc != QDialogButtonBox::Yes) return;
      m_label.setText(m_label.text() + "*v*");
   }
public:
   MyUi(QWidget * parent = {}) : QWidget(parent) {
      m_button.setObjectName("button");
      m_warning.setObjectName("warning");
      m_warning.setWindowModality(Qt::WindowModal);
      m_warning.setInformativeText(
               "Are you sure you want the message to change?");
      m_layout.addWidget(&m_label);
      m_layout.addWidget(&m_button);
      QMetaObject::connectSlotsByName(this);
   }
};

int main(int argc, char *argv[])
{
   QApplication a(argc, argv);
   MyUi ui;
   ui.show();
   return a.exec();
}

#include "main.moc"
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313