Qt: Add message box helpers

Because the standard Qt message boxes look terrible on MacOS.

Using the helpers prevents the stylesheet from breaking liquid ass.

Also styles the dialog so the title isn't hidden.
This commit is contained in:
Stenzek
2025-10-21 21:17:11 +10:00
parent 2fb3773591
commit d9e363f848
2 changed files with 99 additions and 2 deletions

View File

@@ -32,10 +32,10 @@
#include <QtWidgets/QTreeView>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <cmath>
#if defined(_WIN32)
#include "common/windows_headers.h"
@@ -49,7 +49,8 @@ LOG_CHANNEL(Host);
namespace QtUtils {
bool TryMigrateWindowGeometry(SettingsInterface* si, std::string_view window_name, QWidget* widget);
static bool TryMigrateWindowGeometry(SettingsInterface* si, std::string_view window_name, QWidget* widget);
static void SetMessageBoxStyle(QMessageBox* const dlg, Qt::WindowModality modality);
static constexpr const char* WINDOW_GEOMETRY_CONFIG_SECTION = "UI";
@@ -239,6 +240,80 @@ void QtUtils::ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int h
widget->resize(width, height);
}
void QtUtils::SetMessageBoxStyle(QMessageBox* const dlg, Qt::WindowModality modality)
{
dlg->setWindowModality(modality);
#ifdef __APPLE__
// Can't have a stylesheet set even if it doesn't affect the widget.
if (QtHost::NativeThemeStylesheetNeedsUpdate())
{
dlg->setStyleSheet("");
dlg->setAttribute(Qt::WA_StyleSheet, false);
}
#endif
}
QMessageBox::StandardButton QtUtils::MessageBoxIcon(QWidget* parent, QMessageBox::Icon icon, const QString& title,
const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
#ifndef __APPLE__
QMessageBox msgbox(icon, title, text, buttons, parent ? QtUtils::GetRootWidget(parent) : nullptr);
#else
QMessageBox msgbox(icon, QString(), title, buttons, parent ? QtUtils::GetRootWidget(parent) : nullptr);
msgbox.setInformativeText(text);
#endif
// NOTE: Must be application modal, otherwise will lock up on MacOS.
SetMessageBoxStyle(&msgbox, Qt::ApplicationModal);
msgbox.setDefaultButton(defaultButton);
return static_cast<QMessageBox::StandardButton>(msgbox.exec());
}
QMessageBox* QtUtils::NewMessageBox(QMessageBox::Icon icon, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton,
Qt::WindowModality modality, QWidget* parent)
{
#ifndef __APPLE__
QMessageBox* msgbox = new QMessageBox(icon, title, text, buttons, parent ? QtUtils::GetRootWidget(parent) : nullptr);
#else
QMessageBox* msgbox =
new QMessageBox(icon, QString(), title, buttons, parent ? QtUtils::GetRootWidget(parent) : nullptr);
msgbox->setInformativeText(text);
#endif
msgbox->setIcon(icon);
SetMessageBoxStyle(msgbox, modality);
return msgbox;
}
QMessageBox::StandardButton QtUtils::MessageBoxInformation(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return MessageBoxIcon(parent, QMessageBox::Information, title, text, buttons, defaultButton);
}
QMessageBox::StandardButton QtUtils::MessageBoxWarning(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return MessageBoxIcon(parent, QMessageBox::Warning, title, text, buttons, defaultButton);
}
QMessageBox::StandardButton QtUtils::MessageBoxCritical(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return MessageBoxIcon(parent, QMessageBox::Critical, title, text, buttons, defaultButton);
}
QMessageBox::StandardButton QtUtils::MessageBoxQuestion(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton)
{
return MessageBoxIcon(parent, QMessageBox::Question, title, text, buttons, defaultButton);
}
QIcon QtUtils::GetIconForTranslationLanguage(std::string_view language_name)
{
QString icon_path;

View File

@@ -14,6 +14,7 @@
#include <QtCore/QString>
#include <QtCore/QTimer>
#include <QtGui/QIcon>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QWidget>
#include <functional>
#include <initializer_list>
@@ -106,6 +107,27 @@ void SetWindowResizeable(QWidget* widget, bool resizeable);
/// Adjusts the fixed size for a window if it's not resizeable.
void ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int height);
/// Replacement for QMessageBox::question() and friends that doesn't look terrible on MacOS.
QMessageBox::StandardButton MessageBoxInformation(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
QMessageBox::StandardButton MessageBoxWarning(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
QMessageBox::StandardButton MessageBoxCritical(QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons = QMessageBox::Ok,
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
QMessageBox::StandardButton MessageBoxQuestion(
QWidget* parent, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons = QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No),
QMessageBox::StandardButton defaultButton = QMessageBox::NoButton);
QMessageBox::StandardButton MessageBoxIcon(QWidget* parent, QMessageBox::Icon icon, const QString& title,
const QString& text, QMessageBox::StandardButtons buttons,
QMessageBox::StandardButton defaultButton);
QMessageBox* NewMessageBox(QMessageBox::Icon icon, const QString& title, const QString& text,
QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton,
Qt::WindowModality modality, QWidget* parent);
/// Returns icon for language.
QIcon GetIconForTranslationLanguage(std::string_view language_name);