#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.
This commit is contained in:
Stephan Witt 2023-07-16 16:48:49 +02:00
parent b397c78b70
commit b924db72c5
2 changed files with 29 additions and 13 deletions

View File

@ -19,6 +19,8 @@ namespace lyx {
namespace frontend { namespace frontend {
namespace Alert { namespace Alert {
typedef unsigned short buttonid;
/** /**
* Prompt for a question. Returns 0-3 for the chosen button. * Prompt for a question. Returns 0-3 for the chosen button.
* Set default_button and cancel_button to reasonable values. b1-b3 * 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 * "Yes" or "No", I will personally come around to your house and
* slap you with fish, and not in an enjoyable way either. * slap you with fish, and not in an enjoyable way either.
*/ */
int prompt(docstring const & title, docstring const & question, buttonid prompt(docstring const & title, docstring const & question,
int default_button, int cancel_button, buttonid default_button, buttonid cancel_button,
docstring const & b0, docstring const & b1, docstring const & b0, docstring const & b1,
docstring const & b2 = empty_docstring(), docstring const & b2 = empty_docstring(),
docstring const & b3 = empty_docstring()); docstring const & b3 = empty_docstring());

View File

@ -75,8 +75,8 @@ docstring toPlainText(docstring const & msg)
} }
int doPrompt(docstring const & title, docstring const & question, buttonid doPrompt(docstring const & title, docstring const & question,
int default_button, int cancel_button, buttonid default_button, buttonid cancel_button,
docstring const & b1, docstring const & b2, docstring const & b1, docstring const & b2,
docstring const & b3, docstring const & b4) docstring const & b3, docstring const & b4)
{ {
@ -108,7 +108,7 @@ int doPrompt(docstring const & title, docstring const & question,
// FIXME replace that with guiApp->currentView() // FIXME replace that with guiApp->currentView()
//LYXERR0("FOCUS: " << qApp->focusWidget()); //LYXERR0("FOCUS: " << qApp->focusWidget());
QPushButton * b[4] = { 0, 0, 0, 0 }; QPushButton * b[4] = { nullptr, nullptr, nullptr, nullptr };
QMessageBox msg_box(QMessageBox::Information, QMessageBox msg_box(QMessageBox::Information,
toqstr(title), toqstr(question), toqstr(title), toqstr(question),
QMessageBox::NoButton, qApp->focusWidget()); 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); b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
if (!b4.empty()) if (!b4.empty())
b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole); b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
msg_box.setDefaultButton(b[default_button]); if (default_button < size(b) && nullptr != b[default_button])
msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button])); msg_box.setDefaultButton(b[default_button]);
int res = msg_box.exec(); 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(); qApp->restoreOverrideCursor();
if (long_op) if (long_op)
theApp()->startLongOperation(); theApp()->startLongOperation();
// Qt bug: can return -1 on cancel or WM close, despite the docs. size_t res = cancel_button;
if (res == -1)
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; return res;
} }
int prompt(docstring const & title, docstring const & question, buttonid prompt(docstring const & title, docstring const & question,
int default_button, int cancel_button, buttonid default_button, buttonid cancel_button,
docstring const & b0, docstring const & b1, docstring const & b0, docstring const & b1,
docstring const & b2, docstring const & b3) docstring const & b2, docstring const & b3)
{ {