[LyX/master] #12818 correct evaluation of message box result info
Stephan Witt
switt at lyx.org
Sun Jul 16 13:35:57 UTC 2023
commit b924db72c5bc7e194d1b6a2b0e55f0427fa366b6
Author: Stephan Witt <switt at lyx.org>
Date: Sun Jul 16 16:48:49 2023 +0200
#12818 correct evaluation of message box result info
The help page of int QMessageBox::exec() (https://doc.qt.io/qt-6/qmessagebox.html#exec) says:
When using a QMessageBox with standard buttons, this function returns a StandardButton value indicating the standard button that was clicked.
When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.
---
src/frontends/alert.h | 6 ++++--
src/frontends/qt/GuiAlert.cpp | 36 +++++++++++++++++++++++++-----------
2 files changed, 29 insertions(+), 13 deletions(-)
diff --git a/src/frontends/alert.h b/src/frontends/alert.h
index 20da436..a4556ca 100644
--- a/src/frontends/alert.h
+++ b/src/frontends/alert.h
@@ -19,6 +19,8 @@ namespace lyx {
namespace frontend {
namespace Alert {
+typedef unsigned short buttonid;
+
/**
* Prompt for a question. Returns 0-3 for the chosen button.
* Set default_button and cancel_button to reasonable values. b1-b3
@@ -30,8 +32,8 @@ namespace Alert {
* "Yes" or "No", I will personally come around to your house and
* slap you with fish, and not in an enjoyable way either.
*/
-int prompt(docstring const & title, docstring const & question,
- int default_button, int cancel_button,
+buttonid prompt(docstring const & title, docstring const & question,
+ buttonid default_button, buttonid cancel_button,
docstring const & b0, docstring const & b1,
docstring const & b2 = empty_docstring(),
docstring const & b3 = empty_docstring());
diff --git a/src/frontends/qt/GuiAlert.cpp b/src/frontends/qt/GuiAlert.cpp
index 93b8e1d..85a5e22 100644
--- a/src/frontends/qt/GuiAlert.cpp
+++ b/src/frontends/qt/GuiAlert.cpp
@@ -75,8 +75,8 @@ docstring toPlainText(docstring const & msg)
}
-int doPrompt(docstring const & title, docstring const & question,
- int default_button, int cancel_button,
+buttonid doPrompt(docstring const & title, docstring const & question,
+ buttonid default_button, buttonid cancel_button,
docstring const & b1, docstring const & b2,
docstring const & b3, docstring const & b4)
{
@@ -108,7 +108,7 @@ int doPrompt(docstring const & title, docstring const & question,
// FIXME replace that with guiApp->currentView()
//LYXERR0("FOCUS: " << qApp->focusWidget());
- QPushButton * b[4] = { 0, 0, 0, 0 };
+ QPushButton * b[4] = { nullptr, nullptr, nullptr, nullptr };
QMessageBox msg_box(QMessageBox::Information,
toqstr(title), toqstr(question),
QMessageBox::NoButton, qApp->focusWidget());
@@ -120,23 +120,37 @@ int doPrompt(docstring const & title, docstring const & question,
b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
if (!b4.empty())
b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
- msg_box.setDefaultButton(b[default_button]);
- msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
- int res = msg_box.exec();
+ if (default_button < size(b) && nullptr != b[default_button])
+ msg_box.setDefaultButton(b[default_button]);
+ if (cancel_button < size(b) && nullptr != b[cancel_button])
+ msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
+ msg_box.exec();
+ const QAbstractButton * button = msg_box.clickedButton();
qApp->restoreOverrideCursor();
if (long_op)
theApp()->startLongOperation();
- // Qt bug: can return -1 on cancel or WM close, despite the docs.
- if (res == -1)
- res = cancel_button;
+ size_t res = cancel_button;
+
+ if (button == nullptr)
+ return res;
+ else {
+ // Convert selection of the button into an integer
+ for (size_t i = 0; i < size(b); i++) {
+ if (button == b[i]) {
+ res = i;
+ break;
+ }
+ }
+ }
+
return res;
}
-int prompt(docstring const & title, docstring const & question,
- int default_button, int cancel_button,
+buttonid prompt(docstring const & title, docstring const & question,
+ buttonid default_button, buttonid cancel_button,
docstring const & b0, docstring const & b1,
docstring const & b2, docstring const & b3)
{
More information about the lyx-cvs
mailing list