Browse Source
QMessageBox::information() and friends build the box on the stack, parent it to the calling widget and run a nested event loop in exec(). If the parent is closed while the box is open, which the Web API can do at any time, the parent deletes the box and its children and the heap is corrupted. MessageDialog offers the same calls, but the box is heap allocated, given Qt::WA_DeleteOnClose and shown rather than exec'd, so nothing runs a nested event loop and the box owns its own lifetime. A box with the same text as one already up is raised instead of a second being created. The trade off is that information(), warning() and critical() return immediately and cannot report which button was pressed. question() takes a callback for the cases that need the answer. Converts the call sites in the device, channel and feature GUIs. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>chirpmodem-v8.0.0
82 changed files with 343 additions and 120 deletions
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
|
||||
// Some code by AI //
|
||||
// //
|
||||
// This program is free software; you can redistribute it and/or modify //
|
||||
// it under the terms of the GNU General Public License as published by //
|
||||
// the Free Software Foundation as version 3 of the License, or //
|
||||
// (at your option) any later version. //
|
||||
// //
|
||||
// This program is distributed in the hope that it will be useful, //
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
||||
// GNU General Public License V3 for more details. //
|
||||
// //
|
||||
// You should have received a copy of the GNU General Public License //
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <QWidget> |
||||
|
||||
#include "gui/messagedialog.h" |
||||
|
||||
void MessageDialog::show(QWidget *parent, QMessageBox::Icon icon, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, std::function<void (QMessageBox::StandardButton)> onAnswer) |
||||
{ |
||||
// Avoid creating new box with same text. Raise the existing one instead.
|
||||
if (parent) |
||||
{ |
||||
const QList<QMessageBox *> existing = parent->findChildren<QMessageBox *>( |
||||
QString(), Qt::FindDirectChildrenOnly); |
||||
|
||||
for (QMessageBox *box : existing) |
||||
{ |
||||
if (box->isVisible() && (box->text() == text) && (box->windowTitle() == title)) |
||||
{ |
||||
box->raise(); |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
|
||||
QMessageBox *box = new QMessageBox(icon, title, text, buttons, parent); |
||||
box->setAttribute(Qt::WA_DeleteOnClose); |
||||
|
||||
if (onAnswer) |
||||
{ |
||||
QObject::connect(box, &QMessageBox::finished, box, [box, onAnswer](int) { |
||||
onAnswer(box->standardButton(box->clickedButton())); |
||||
}); |
||||
} |
||||
|
||||
// open() rather than exec(): it returns immediately, so nothing left on stack
|
||||
box->open(); |
||||
} |
||||
|
||||
void MessageDialog::information(QWidget *parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons) |
||||
{ |
||||
show(parent, QMessageBox::Information, title, text, buttons, nullptr); |
||||
} |
||||
|
||||
void MessageDialog::warning(QWidget *parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons) |
||||
{ |
||||
show(parent, QMessageBox::Warning, title, text, buttons, nullptr); |
||||
} |
||||
|
||||
void MessageDialog::critical(QWidget *parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons) |
||||
{ |
||||
show(parent, QMessageBox::Critical, title, text, buttons, nullptr); |
||||
} |
||||
|
||||
void MessageDialog::question(QWidget *parent, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, std::function<void (QMessageBox::StandardButton)> onAnswer) |
||||
{ |
||||
show(parent, QMessageBox::Question, title, text, buttons, onAnswer); |
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
|
||||
// Some code by AI //
|
||||
// //
|
||||
// This program is free software; you can redistribute it and/or modify //
|
||||
// it under the terms of the GNU General Public License as published by //
|
||||
// the Free Software Foundation as version 3 of the License, or //
|
||||
// (at your option) any later version. //
|
||||
// //
|
||||
// This program is distributed in the hope that it will be useful, //
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
||||
// GNU General Public License V3 for more details. //
|
||||
// //
|
||||
// You should have received a copy of the GNU General Public License //
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef SDRGUI_GUI_MESSAGEDIALOG_H |
||||
#define SDRGUI_GUI_MESSAGEDIALOG_H |
||||
|
||||
#include <QMessageBox> |
||||
#include <QString> |
||||
|
||||
#include <functional> |
||||
|
||||
#include "export.h" |
||||
|
||||
class QWidget; |
||||
|
||||
// Non-blocking replacements for the QMessageBox static functions, for use in device,
|
||||
// channel and feature GUIs.
|
||||
//
|
||||
// QMessageBox::information() and friends build the message box on the stack, parent it to
|
||||
// the calling widget and run a nested event loop in exec(). If the parent is closed
|
||||
// via the Web API while the box is open, the parent deletes the box and its children,
|
||||
// which corrupts the heap.
|
||||
//
|
||||
// The trade-off is that these return immediately, so they cannot report which button was
|
||||
// pressed. Use question() when the answer is needed; it delivers it to a callback.
|
||||
class SDRGUI_API MessageDialog |
||||
{ |
||||
public: |
||||
static void information(QWidget *parent, const QString& title, const QString& text, |
||||
QMessageBox::StandardButtons buttons = QMessageBox::Ok); |
||||
static void warning(QWidget *parent, const QString& title, const QString& text, |
||||
QMessageBox::StandardButtons buttons = QMessageBox::Ok); |
||||
static void critical(QWidget *parent, const QString& title, const QString& text, |
||||
QMessageBox::StandardButtons buttons = QMessageBox::Ok); |
||||
|
||||
//!< Calls onAnswer with the button pressed. Not called if the box is destroyed unanswered
|
||||
static void question(QWidget *parent, const QString& title, const QString& text, |
||||
QMessageBox::StandardButtons buttons, std::function<void (QMessageBox::StandardButton)> onAnswer); |
||||
|
||||
private: |
||||
static void show(QWidget *parent, QMessageBox::Icon icon, const QString& title, |
||||
const QString& text, QMessageBox::StandardButtons buttons, |
||||
std::function<void (QMessageBox::StandardButton)> onAnswer); |
||||
}; |
||||
|
||||
#endif // SDRGUI_GUI_MESSAGEDIALOG_H
|
||||
Loading…
Reference in new issue