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.
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.
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:
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()
.
Standard buttons are used.
Both text and informative text are provided to conform with human interface guidelines across platforms.
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.
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.
Default member initializers are used when constructing members.
Widgets that are added to layouts are not passed a parent. Doing so would be redundant.
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"