View Issue Details

IDProjectCategoryView StatusLast Update
0036639LazarusWidgetsetpublic2020-05-17 19:16
ReporterZeljan Rikalo Assigned ToZeljan Rikalo  
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
Product Version2.1 (SVN) 
Summary0036639: Qt5: Dark mode theme under windows 10
DescriptionQt5 does not support yet dark mode under windows 10 (at least current 5.14), so I've attached patch for Qt5 widgetset which mimics dark mode without stylesheet and with help of fusion theme.
TagsNo tags attached.
Fixed in Revision
LazTarget
WidgetsetQT5
Attached Files

Relationships

related to 0033440 new [FEATURE REQUEST] Change the theme of entire IDE 

Activities

Zeljan Rikalo

2020-01-30 14:15

developer  

qt5win10darkmode.diff (15,475 bytes)   
Index: qtint.pp
===================================================================
--- qtint.pp	(revision 62594)
+++ qtint.pp	(working copy)
@@ -31,6 +31,7 @@
 uses 
   {$IFDEF MSWINDOWS}
   Windows, // used to retrieve correct caption color values
+  Registry, // used to retreive if we are using dark mode theme
   {$ENDIF}
   // Bindings - qt5 must come first to avoid type redefinition problems
   qt5,
@@ -110,6 +111,9 @@
     procedure QtRestoreStayOnTop(const ASystemTopAlso: Boolean = False);
     procedure SetDefaultAppFontName;
   protected
+    {$IFDEF MSWINDOWS}
+    FDarkStyle: QStyleH;
+    {$ENDIF}
     FStockNullBrush: HBRUSH;
     FStockBlackBrush: HBRUSH;
     FStockLtGrayBrush: HBRUSH;
@@ -208,6 +212,7 @@
     {$ENDIF}
     {$IFDEF MSWINDOWS}
     function GetWinKeyState(AKeyState: LongInt): SHORT;
+    procedure ApplyDarkMode(AWidget: QWidgetH);
     {$ENDIF}
     function CreateDefaultFont: HFONT; virtual;
     function GetDefaultAppFontName: WideString;
Index: qtobject.inc
===================================================================
--- qtobject.inc	(revision 62594)
+++ qtobject.inc	(working copy)
@@ -171,6 +171,14 @@
 procedure TQtWidgetSet.AppInit(var ScreenInfo: TScreenInfo);
 var
   ScreenDC: HDC;
+  {$IFDEF MSWINDOWS}
+  APalette: QPaletteH;
+  AColor: TQColor;
+  AQtColor: QColorH;
+  WStr: WideString;
+  AReg: TRegistry;
+  ADarkEnabled: boolean;
+  {$ENDIF}
 begin
   WakeMainThread := @OnWakeMainThread;
 
@@ -203,8 +211,141 @@
     FAppSessionQuit := nil;
     FAppSaveSessionRequest := nil;
   end;
+  {$IFDEF MSWINDOWS}
+  if not IsLibraryInstance then
+    FDarkStyle := nil;
+  if StyleName = 'windowsvista' then
+  begin
+    ADarkEnabled := False;
+    AReg := TRegistry.Create(KEY_READ);
+    try
+      AReg.RootKey := HKEY_CURRENT_USER;
+      if AReg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize') then
+      begin
+        if AReg.ValueExists('AppsUseLightTheme') then
+          ADarkEnabled := AReg.ReadInteger('AppsUseLightTheme') = 0;
+      end;
+    finally
+      AReg.Free;
+    end;
 
+    if ADarkEnabled then
+    begin
+      WStr := 'fusion';
+      if not IsLibraryInstance then
+        FDarkStyle := QStyleFactory_create(@WStr);
+      APalette := QPalette_create();
+      try
+        AQtColor := QColor_Create(53,53,53);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteWindow, PQColor(@AColor));
 
+        AQtColor := QColor_Create(Qtwhite);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteWindowText, PQColor(@AColor));
+
+
+        AQtColor := QColor_Create(127,127,127);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteDisabled, QPaletteWindowText, PQColor(@AColor));
+
+
+        AQtColor := QColor_Create(42,42,42);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteBase, PQColor(@AColor));
+
+        AQtColor := QColor_Create(66,66,66);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteAlternateBase, PQColor(@AColor));
+
+
+        AQtColor := QColor_Create(Qtblack);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteToolTipBase, PQColor(@AColor));
+
+        AQtColor := QColor_Create(Qtwhite);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteToolTipText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(Qtwhite);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(127,127,127);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteDisabled, QPaletteText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(35,35,35);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteDark, PQColor(@AColor));
+
+        AQtColor := QColor_Create(20,20,20);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteShadow, PQColor(@AColor));
+
+        AQtColor := QColor_Create(53,53,53);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteButton, PQColor(@AColor));
+
+        AQtColor := QColor_Create(Qtwhite);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteButtonText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(127,127,127);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteDisabled, QPaletteButtonText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(Qtred);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteBrightText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(42,130,218);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteLink, PQColor(@AColor));
+
+        AQtColor := QColor_Create(42,130,218);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteHighlight, PQColor(@AColor));
+
+        AQtColor := QColor_Create(80,80,80);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteDisabled, QPaletteHighlight, PQColor(@AColor));
+
+        AQtColor := QColor_Create(Qtwhite);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteHighlightedText, PQColor(@AColor));
+
+        AQtColor := QColor_Create(127,127,127);
+        QColor_convertTo(AQtColor, @AColor, QColorRgb);
+        QColor_destroy(AQtColor);
+        QPalette_setColor(APalette, QPaletteDisabled, QPaletteHighlightedText, PQColor(@AColor));
+        QApplication_setPalette(APalette, nil);
+      finally
+        QPalette_destroy(APalette);
+      end;
+    end;
+  end;
+  {$ENDIF}
+
   ScreenDC := GetDC(0);
   try
     {$IFDEF DARWIN}
@@ -333,6 +474,13 @@
     end;
     {$ENDIF}
     QCoreApplication_quit;
+    {$IFDEF MSWINDOWS}
+    if Assigned(FDarkStyle) then
+    begin
+      QObject_destroy(FDarkStyle);
+      FDarkStyle := nil;
+    end;
+    {$ENDIF}
   end;
 end;
 
@@ -1692,6 +1840,12 @@
 begin
   Result := Windows.GetKeyState(AKeyState);
 end;
+
+procedure TQtWidgetSet.ApplyDarkMode(AWidget: QWidgetH);
+begin
+  if Assigned(FDarkStyle) then
+    QWidget_setStyle(AWidget, FDarkStyle);
+end;
 {$ENDIF}
 
 {------------------------------------------------------------------------------
Index: qtpagecontrol.inc
===================================================================
--- qtpagecontrol.inc	(revision 62594)
+++ qtpagecontrol.inc	(working copy)
@@ -37,6 +37,11 @@
   {$endif}
 
   QtPage := TQtPage.Create(AWinControl, AParams);
+
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtPage.Widget);
+  {$ENDIF}
+
   QtPage.AttachEvents;
 
   // Returns the Handle
@@ -169,6 +174,9 @@
   begin
     QtTTabCtl := TQtWidget.Create(AWinControl, AParams);
     QtTTabCtl.ChildOfComplexWidget := ccwTTabControl;
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(QtTTabCtl.Widget);
+    {$ENDIF}
     QtTTabCtl.AttachEvents;
     Result := TLCLIntfHandle(QtTTabCtl);
   end else
@@ -179,6 +187,10 @@
     {$IFDEF DARWIN}
     QTabWidget_setElideMode(QTabWidgetH(QtTabWidget.Widget), QtElideNone);
     {$ENDIF}
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(QtTabWidget.Widget);
+    {$ENDIF}
+
     QtTabWidget.AttachEvents;
     Result := TLCLIntfHandle(QtTabWidget);
   end;
Index: qtwidgets.pas
===================================================================
--- qtwidgets.pas	(revision 62594)
+++ qtwidgets.pas	(working copy)
@@ -10651,6 +10651,11 @@
     {$ENDIF}
     FTabBar.FSavedIndexOnPageChanging := Allow_TCN_SELCHANGE;
     FTabBar.FOwner := Self;
+
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(FTabBar.Widget);
+    {$ENDIF}
+
     FTabBar.AttachEvents;
   end;
   Result := FTabBar;
@@ -11068,6 +11073,9 @@
       FLineEdit.FOwner := Self;
       QObject_disconnect(FLineEdit.Widget, '2returnPressed()', Widget, '1_q_returnPressed()');
       FLineEdit.ChildOfComplexWidget := ccwComboBox;
+      {$IFDEF MSWINDOWS}
+      QtWidgetSet.ApplyDarkMode(FLineEdit.Widget);
+      {$ENDIF}
       FLineEdit.AttachEvents;
     end;
   end;
@@ -11092,6 +11100,7 @@
     FDropList.ChildOfComplexWidget := ccwComboBox;
     QComboBox_setModel(QComboBoxH(Widget), FDropList.getModel);
     QComboBox_setView(QComboBoxH(Widget), QListWidgetH(FDropList.Widget));
+
     FDropList.AttachEvents;
   end;
   Result := FDropList;
@@ -11825,6 +11834,11 @@
     QObject_hook_hook_events(FLineEditHook, @LineEditEventFilter);
     FTextChangedHook := QLineEdit_hook_create(FLineEdit);
     QLineEdit_hook_hook_textChanged(FTextChangedHook, @SignalLineEditTextChanged);
+
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(FLineEdit);
+    {$ENDIF}
+
   end;
   Result := FLineEdit;
 end;
@@ -15131,6 +15145,11 @@
     QHeaderView_hook_hook_sortIndicatorChanged(FSortChanged,
       @SignalSortIndicatorChanged);
     {$ENDIF}
+
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(FHeader.Widget);
+    {$ENDIF}
+
     FHeader.AttachEvents;
   end;
   Result := FHeader;
@@ -17374,6 +17393,9 @@
     FHScrollBar.FOwner := Self;
     FHScrollBar.setFocusPolicy(QtNoFocus);
 
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(FHScrollbar.Widget);
+    {$ENDIF}
     if not FHScrollBar.CanChangeFontColor then
     begin
       with FHScrollBar do
@@ -17409,6 +17431,11 @@
     FVScrollBar.ChildOfComplexWidget := ccwAbstractScrollArea;
     FVScrollBar.FOwner := Self;
     FVScrollBar.setFocusPolicy(QtNoFocus);
+
+    {$IFDEF MSWINDOWS}
+    QtWidgetSet.ApplyDarkMode(FVScrollbar.Widget);
+    {$ENDIF}
+
     if not FVScrollBar.CanChangeFontColor then
     begin
       with FVScrollBar do
Index: qtwsbuttons.pp
===================================================================
--- qtwsbuttons.pp	(revision 62594)
+++ qtwsbuttons.pp	(working copy)
@@ -24,7 +24,7 @@
 uses
   // Libs
   qt5,
-  qtwidgets, qtobjects,
+  qtwidgets, qtobjects, qtint,
   // RTL
   SysUtils, Types,
   // LCL
@@ -64,6 +64,9 @@
   QtBitBtn: TQtBitBtn;
 begin
   QtBitBtn := TQtBitBtn.Create(AWinControl, AParams);
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtBitBtn.Widget);
+  {$ENDIF}
   QtBitBtn.AttachEvents;
   Result := TLCLIntfHandle(QtBitBtn);
 end;
Index: qtwscomctrls.pp
===================================================================
--- qtwscomctrls.pp	(revision 62594)
+++ qtwscomctrls.pp	(working copy)
@@ -606,6 +606,11 @@
 
   RecreatePanels(TStatusBar(AWinControl), QtStatusBar);
 
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtStatusBar.Widget);
+  {$ENDIF}
+
+
   QtStatusBar.AttachEvents;
 
   // Return handle
@@ -742,6 +747,7 @@
 
     QtListWidget.Checkable := TCustomListView(AWinControl).Checkboxes;
     QtListWidget.OwnerDrawn := ALV.IsCustomDrawn(dtControl, cdPrePaint);
+
     QtListWidget.AttachEvents;
     Result := TLCLIntfHandle(QtListWidget);
   end else
Index: qtwsspin.pp
===================================================================
--- qtwsspin.pp	(revision 62594)
+++ qtwsspin.pp	(working copy)
@@ -24,7 +24,7 @@
 uses
   // Bindings
   qt5,
-  qtwidgets,
+  qtwidgets, qtint,
   // LCL
   Spin, SysUtils, Controls, Classes, LCLType, LCLProc, LCLIntf, Forms, StdCtrls,
   //RTL
@@ -115,6 +115,10 @@
   QtSpinBox.setBorder(TCustomFloatSpinEdit(AWinControl).BorderStyle = bsSingle);
   InternalUpdateControl(QtSpinBox, TCustomFloatSpinEdit(AWinControl));
 
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtSpinBox.Widget);
+  {$ENDIF}
+
   QtSpinBox.AttachEvents;
   
   Result := TLCLIntfHandle(QtSpinBox);
Index: qtwsstdctrls.pp
===================================================================
--- qtwsstdctrls.pp	(revision 62594)
+++ qtwsstdctrls.pp	(working copy)
@@ -305,6 +305,10 @@
 begin
   QtScrollBar := TQtScrollBar.Create(AWinControl, AParams);
 
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtScrollBar.Widget);
+  {$ENDIF}
+
   QtScrollBar.AttachEvents;
   
   case TScrollBar(AWinControl).Kind of
@@ -844,6 +848,10 @@
   QtLineEdit.setBorder(TCustomEdit(AWinControl).BorderStyle = bsSingle);
   QtLineEdit.setAlignment(AlignmentMap[TCustomEdit(AWinControl).Alignment]);
   QtLineEdit.NumbersOnly := TCustomEdit(AWinControl).NumbersOnly;
+
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtLineEdit.Widget);
+  {$ENDIF}
   QtLineEdit.AttachEvents;
 
   Result := TLCLIntfHandle(QtLineEdit);
@@ -1097,6 +1105,11 @@
 begin
   QtStaticText := TQtStaticText.Create(AWinControl, AParams);
   QtStaticText.WordWrap := True;
+
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtStaticText.Widget);
+  {$ENDIF}
+
   QtStaticText.AttachEvents;
   QtStaticText.setAlignment(AlignmentMap[TCustomStaticText(AWinControl).Alignment]);
   QtStaticText.setFrameShape(StaticBorderFrameShapeMap[TCustomStaticText(AWinControl).BorderStyle]);
@@ -1140,6 +1153,11 @@
   QtPushButton: TQtPushButton;
 begin
   QtPushButton := TQtPushButton.Create(AWinControl, AParams);
+
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtPushButton.Widget);
+  {$ENDIF}
+
   QtPushButton.AttachEvents;
 
   // Returns the Handle
@@ -1229,6 +1247,11 @@
 begin
   QtCheckBox := TQtCheckBox.Create(AWinControl, AParams);
   QtCheckBox.setTriState(TCustomCheckBox(AWinControl).AllowGrayed);
+
+  {$IFDEF MSWINDOWS}
+  // QtWidgetSet.ApplyDarkMode(QtCheckBox.Widget);
+  {$ENDIF}
+
   QtCheckBox.AttachEvents;
 
   Result := TLCLIntfHandle(QtCheckBox);
@@ -1291,6 +1314,11 @@
   QtRadioButton: TQtRadioButton;
 begin
   QtRadioButton := TQtRadioButton.Create(AWinControl, AParams);
+
+  {$IFDEF MSWINDOWS}
+  //QtWidgetSet.ApplyDarkMode(QtRadioButton.Widget);
+  {$ENDIF}
+
   QtRadioButton.AttachEvents;
 
   Result := TLCLIntfHandle(QtRadioButton);
@@ -1381,6 +1409,11 @@
     (AParams.Style and CBS_SIMPLE <> 0));
 
   QtComboBox.DropList.setUniformItemSizes(AParams.Style and CBS_OWNERDRAWFIXED <> 0);
+
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtComboBox.Widget);
+  {$ENDIF}
+
   QtComboBox.AttachEvents;
   QtComboBox.OwnerDrawn := (AParams.Style and CBS_OWNERDRAWFIXED <> 0) or
     (AParams.Style and CBS_OWNERDRAWVARIABLE <> 0);
@@ -1730,6 +1763,10 @@
 begin
   QtToggleBox := TQtToggleBox.Create(AWinControl, AParams);
   QtToggleBox.setCheckable(True);
+
+  {$IFDEF MSWINDOWS}
+  QtWidgetSet.ApplyDarkMode(QtToggleBox.Widget);
+  {$ENDIF}
   QtToggleBox.AttachEvents;
   
   Result := TLCLIntfHandle(QtToggleBox);
qt5win10darkmode.diff (15,475 bytes)   

Issue History

Date Modified Username Field Change
2020-01-30 14:15 Zeljan Rikalo New Issue
2020-01-30 14:15 Zeljan Rikalo Status new => assigned
2020-01-30 14:15 Zeljan Rikalo Assigned To => Zeljan Rikalo
2020-01-30 14:15 Zeljan Rikalo File Added: qt5win10darkmode.diff
2020-05-17 13:50 Martin Friebe Relationship added related to 0033440