Skip to content

Commit cca43a5

Browse files
committed
Qt: Set QStyleHints colorScheme property properly
1 parent b02318e commit cca43a5

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

pcsx2-qt/Debugger/DisassemblyView.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ inline QString DisassemblyView::DisassemblyStringFromAddress(u32 address, QFont
939939
QColor DisassemblyView::GetAddressFunctionColor(u32 address)
940940
{
941941
std::array<QColor, 6> colors;
942-
if (QtUtils::IsLightTheme(palette()))
942+
if (!QtHost::IsDarkApplicationTheme())
943943
{
944944
colors = {
945945
QColor::fromRgba(0xFFFA3434),

pcsx2-qt/Debugger/Docking/DropIndicators.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "DropIndicators.h"
55

6+
#include "QtHost.h"
67
#include "QtUtils.h"
78
#include "Debugger/Docking/DockViews.h"
89

@@ -21,7 +22,7 @@ static std::pair<QColor, QColor> pickNiceColours(const QPalette& palette, bool h
2122
QColor fill = palette.highlight().color();
2223
QColor outline = palette.highlight().color();
2324

24-
if (QtUtils::IsLightTheme(palette))
25+
if (!QtHost::IsDarkApplicationTheme())
2526
{
2627
fill = fill.darker(200);
2728
outline = outline.darker(200);
@@ -197,7 +198,7 @@ static const constexpr int INDICATOR_MARGIN = 10;
197198
static bool isWayland()
198199
{
199200
return KDDockWidgets::Core::Platform::instance()->displayType() ==
200-
KDDockWidgets::Core::Platform::DisplayType::Wayland;
201+
KDDockWidgets::Core::Platform::DisplayType::Wayland;
201202
}
202203

203204
static QWidget* parentForIndicatorWindow(KDDockWidgets::Core::ClassicDropIndicatorOverlay* classic_indicators)

pcsx2-qt/QtUtils.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ namespace QtUtils
9090

9191
const int min_column_width = header->minimumSectionSize();
9292
const int scrollbar_width = ((view->verticalScrollBar() && view->verticalScrollBar()->isVisible()) ||
93-
view->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOn) ? view->verticalScrollBar()->width() : 0;
93+
view->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOn) ?
94+
view->verticalScrollBar()->width() :
95+
0;
9496
int num_flex_items = 0;
9597
int total_width = 0;
9698
int column_index = 0;
@@ -346,11 +348,6 @@ namespace QtUtils
346348
return csv;
347349
}
348350

349-
bool IsLightTheme(const QPalette& palette)
350-
{
351-
return palette.text().color().lightnessF() < 0.5;
352-
}
353-
354351
bool IsCompositorManagerRunning()
355352
{
356353
if (qEnvironmentVariableIsSet("PCSX2_NO_COMPOSITING"))

pcsx2-qt/QtUtils.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ namespace QtUtils
9696
/// Converts an abstract item model to a CSV string.
9797
QString AbstractItemModelToCSV(QAbstractItemModel* model, int role = Qt::DisplayRole, bool useQuotes = false);
9898

99-
// Heuristic to check if the current theme is a light or dark theme.
100-
bool IsLightTheme(const QPalette& palette);
101-
99+
/// Checks if we can use transparency effects e.g. for dock drop indicators.
102100
bool IsCompositorManagerRunning();
103101

104102
/// Sets the scalable icon to a given label (svg icons, or icons with multiple size pixmaps)

pcsx2-qt/Themes.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
#include <QtCore/QFile>
1212
#include <QtGui/QPalette>
1313
#include <QtGui/QPixmapCache>
14+
#include <QtGui/QStyleHints>
1415
#include <QtWidgets/QApplication>
1516
#include <QtWidgets/QStyle>
1617
#include <QtWidgets/QStyleFactory>
1718

1819
namespace QtHost
1920
{
2021
static void SetStyleFromSettings();
22+
static void SetColorScheme(Qt::ColorScheme colour_scheme);
2123
} // namespace QtHost
2224

2325
static QString s_unthemed_style_name;
@@ -54,8 +56,12 @@ void QtHost::UpdateApplicationTheme()
5456

5557
bool QtHost::IsDarkApplicationTheme()
5658
{
59+
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
60+
return qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark;
61+
#else
5762
QPalette palette = qApp->palette();
58-
return (palette.windowText().color().value() > palette.window().color().value());
63+
return palette.windowText().color().value() > palette.window().color().value();
64+
#endif
5965
}
6066

6167
void QtHost::SetIconThemeFromStyle()
@@ -73,13 +79,15 @@ void QtHost::SetStyleFromSettings()
7379
qApp->setStyle(QStyleFactory::create("Fusion"));
7480
qApp->setPalette(s_unthemed_palette);
7581
qApp->setStyleSheet(QString());
82+
SetColorScheme(Qt::ColorScheme::Unknown);
7683
}
7784
#ifdef _WIN32
7885
else if (theme == "windowsvista")
7986
{
8087
qApp->setStyle(QStyleFactory::create("windowsvista"));
8188
qApp->setPalette(s_unthemed_palette);
8289
qApp->setStyleSheet(QString());
90+
SetColorScheme(Qt::ColorScheme::Unknown);
8391
}
8492
#endif
8593
else if (theme == "darkfusion")
@@ -116,6 +124,7 @@ void QtHost::SetStyleFromSettings()
116124

117125
qApp->setPalette(darkPalette);
118126
qApp->setStyleSheet(QString());
127+
SetColorScheme(Qt::ColorScheme::Dark);
119128
}
120129
else if (theme == "darkfusionblue")
121130
{
@@ -151,6 +160,7 @@ void QtHost::SetStyleFromSettings()
151160

152161
qApp->setPalette(darkBluePalette);
153162
qApp->setStyleSheet(QString());
163+
SetColorScheme(Qt::ColorScheme::Dark);
154164
}
155165
else if (theme == "GreyMatter")
156166
{
@@ -187,6 +197,7 @@ void QtHost::SetStyleFromSettings()
187197

188198
qApp->setPalette(greyMatterPalette);
189199
qApp->setStyleSheet(QString());
200+
SetColorScheme(Qt::ColorScheme::Dark);
190201
}
191202
else if (theme == "UntouchedLagoon")
192203
{
@@ -222,6 +233,7 @@ void QtHost::SetStyleFromSettings()
222233

223234
qApp->setPalette(untouchedLagoonPalette);
224235
qApp->setStyleSheet(QString());
236+
SetColorScheme(Qt::ColorScheme::Light);
225237
}
226238
else if (theme == "BabyPastel")
227239
{
@@ -259,6 +271,7 @@ void QtHost::SetStyleFromSettings()
259271

260272
qApp->setPalette(babyPastelPalette);
261273
qApp->setStyleSheet(QString());
274+
SetColorScheme(Qt::ColorScheme::Light);
262275
}
263276
else if (theme == "PizzaBrown")
264277
{
@@ -296,6 +309,7 @@ void QtHost::SetStyleFromSettings()
296309

297310
qApp->setPalette(pizzaPalette);
298311
qApp->setStyleSheet(QString());
312+
SetColorScheme(Qt::ColorScheme::Light);
299313
}
300314
else if (theme == "PCSX2Blue")
301315
{
@@ -331,6 +345,7 @@ void QtHost::SetStyleFromSettings()
331345

332346
qApp->setPalette(pcsx2BluePalette);
333347
qApp->setStyleSheet(QString());
348+
SetColorScheme(Qt::ColorScheme::Light);
334349
}
335350
else if (theme == "ScarletDevilRed")
336351
{
@@ -364,6 +379,7 @@ void QtHost::SetStyleFromSettings()
364379

365380
qApp->setPalette(scarletDevilPalette);
366381
qApp->setStyleSheet(QString());
382+
SetColorScheme(Qt::ColorScheme::Dark);
367383
}
368384
else if (theme == "VioletAngelPurple")
369385
{
@@ -397,6 +413,7 @@ void QtHost::SetStyleFromSettings()
397413

398414
qApp->setPalette(violetAngelPalette);
399415
qApp->setStyleSheet(QString());
416+
SetColorScheme(Qt::ColorScheme::Dark);
400417
}
401418
else if (theme == "CobaltSky")
402419
{
@@ -434,6 +451,7 @@ void QtHost::SetStyleFromSettings()
434451

435452
qApp->setPalette(cobaltSkyPalette);
436453
qApp->setStyleSheet(QString());
454+
SetColorScheme(Qt::ColorScheme::Dark);
437455
}
438456
else if (theme == "AMOLED")
439457
{
@@ -470,6 +488,7 @@ void QtHost::SetStyleFromSettings()
470488

471489
qApp->setPalette(AMOLEDPalette);
472490
qApp->setStyleSheet(QString());
491+
SetColorScheme(Qt::ColorScheme::Dark);
473492
}
474493
else if (theme == "Ruby")
475494
{
@@ -503,6 +522,7 @@ void QtHost::SetStyleFromSettings()
503522

504523
qApp->setPalette(rubyPalette);
505524
qApp->setStyleSheet(QString());
525+
SetColorScheme(Qt::ColorScheme::Dark);
506526
}
507527
else if (theme == "Sapphire")
508528
{
@@ -536,6 +556,7 @@ void QtHost::SetStyleFromSettings()
536556

537557
qApp->setPalette(sapphirePalette);
538558
qApp->setStyleSheet(QString());
559+
SetColorScheme(Qt::ColorScheme::Dark);
539560
}
540561
else if (theme == "Emerald")
541562
{
@@ -569,6 +590,7 @@ void QtHost::SetStyleFromSettings()
569590

570591
qApp->setPalette(emeraldPalette);
571592
qApp->setStyleSheet(QString());
593+
SetColorScheme(Qt::ColorScheme::Dark);
572594
}
573595
else if (theme == "Custom")
574596
{
@@ -587,11 +609,21 @@ void QtHost::SetStyleFromSettings()
587609
{
588610
qApp->setStyle(QStyleFactory::create("Fusion"));
589611
}
612+
613+
SetColorScheme(Qt::ColorScheme::Unknown);
590614
}
591615
else
592616
{
593617
qApp->setStyle(s_unthemed_style_name);
594618
qApp->setPalette(s_unthemed_palette);
595619
qApp->setStyleSheet(QString());
620+
SetColorScheme(Qt::ColorScheme::Unknown);
596621
}
597622
}
623+
624+
static void QtHost::SetColorScheme(Qt::ColorScheme colour_scheme)
625+
{
626+
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
627+
qApp->styleHints()->setColorScheme(colour_scheme);
628+
#endif
629+
}

0 commit comments

Comments
 (0)