From e5ba6d939319b9c5540f963b720e5ce6bfaa162e Mon Sep 17 00:00:00 2001 From: Graham Dawes Date: Fri, 28 Jun 2019 13:13:39 +0100 Subject: [PATCH] Implement SetForegroundColour and SetBackgroundColour for wxQt Closes https://github.com/wxWidgets/wxWidgets/pull/1373 --- include/wx/qt/window.h | 3 +++ src/qt/window.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/wx/qt/window.h b/include/wx/qt/window.h index 681a28732e..98608b1fa2 100644 --- a/include/wx/qt/window.h +++ b/include/wx/qt/window.h @@ -131,6 +131,9 @@ public: virtual bool SetTransparent(wxByte alpha) wxOVERRIDE; virtual bool CanSetTransparent() wxOVERRIDE { return true; } + virtual bool SetBackgroundColour(const wxColour& colour) wxOVERRIDE; + virtual bool SetForegroundColour(const wxColour& colour) wxOVERRIDE; + QWidget *GetHandle() const wxOVERRIDE; #if wxUSE_DRAG_AND_DROP diff --git a/src/qt/window.cpp b/src/qt/window.cpp index aabf049ad8..7ccb774ab5 100644 --- a/src/qt/window.cpp +++ b/src/qt/window.cpp @@ -347,8 +347,8 @@ void wxWindowQt::PostCreation(bool generic) // // Set the default color so Paint Event default handler clears the DC: - SetBackgroundColour(wxColour(GetHandle()->palette().background().color())); - SetForegroundColour(wxColour(GetHandle()->palette().foreground().color())); + wxWindowBase::SetBackgroundColour(wxColour(GetHandle()->palette().background().color())); + wxWindowBase::SetForegroundColour(wxColour(GetHandle()->palette().foreground().color())); GetHandle()->setFont( wxWindowBase::GetFont().GetHandle() ); @@ -1078,6 +1078,42 @@ bool wxWindowQt::SetTransparent(wxByte alpha) } +namespace +{ + +void wxQtChangeRoleColour(QPalette::ColorRole role, + QWidget* widget, + const wxColour& colour) +{ + QPalette palette = widget->palette(); + palette.setColor(role, colour.GetQColor()); + widget->setPalette(palette); +} + +} // anonymous namespace + +bool wxWindowQt::SetBackgroundColour(const wxColour& colour) +{ + if ( !wxWindowBase::SetBackgroundColour(colour) ) + return false; + + QWidget *widget = GetHandle(); + wxQtChangeRoleColour(widget->backgroundRole(), widget, colour); + + return true; +} + +bool wxWindowQt::SetForegroundColour(const wxColour& colour) +{ + if (!wxWindowBase::SetForegroundColour(colour)) + return false; + + QWidget *widget = GetHandle(); + wxQtChangeRoleColour(widget->foregroundRole(), widget, colour); + + return true; +} + bool wxWindowQt::QtHandlePaintEvent ( QWidget *handler, QPaintEvent *event ) { /* If this window has scrollbars, only let wx handle the event if it is