Forum > QT

Qt colors

(1/2) > >>

dbannon:
As I understand it Lazarus Apps use Lazarus default colours unless the user has somehow set something else. Doing so makes the app really flexible, especially for people who like dark themes, they can get a great result. A Qt app does not pickup system colours by default (sadly).

But if the user does "set something", see below, Qt LCL follows the set theme really well but some other, external, components do not. It would be easy to work around that or fix the component if we could just read what the colors in the chosen theme really are. I assume there is some qt library that reads the (eg) qt5ct theme and Qt widgets get their colours from there.

Does anyone know how to read colours from the selected theme ? 

More details ?
Starting a Qt app with either an env var set or a switch will instruct it what colour theme to use -

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---$> QT_QPA_PLATFORMTHEME=qt5ct ./my_qt5_app <enter> $> ./my_qt5_app  -platformtheme  gtk2 <enter>
If you use qt5ct then it makes sense to set that var in either /etc/environment (requires root or, simpler in a .xsessionrc file in your home dir). Or use the switch for a one off test. In either case the value can be, eg qt5ct (if installed and configed) or gtk2.

qt5ct is really good but does require an extra package and a bit of configuring. Just setting gtk2 might be enough for most people but it does not always work. I just noticed that it does not work on Debian Bookwork at the moment. Sigh....

So, installing qt5ct seems safer ! It adds about 180k to your install. Make it a "recommended" attribute of a deb and it will be installed along with the other Qt libraries.  .

Davo

EDIT : added warning about value=gtk2 not always working.


zeljko:
You can read theme colors from QApplication_palette(), read docs about QPalette.
Also, you can create your own QPalette, set your colors and set it as application palette.

dbannon:
Thanks Zelko, I did read the docs about QPalette and decided it would be easier to directly read qt5ct's config !  I am guessing thats how QPalette does it so its doubling up. But only a 100 lines of code and I am feeling lazy !

Thanks !

Davo

Edit - in the unlikely case of someone wanting to do what I needed to -
 https://github.com/tomboy-notes/tomboy-ng/blob/master/source/uqt_colors.pas

rca:

--- Quote from: dbannon on March 12, 2023, 05:53:21 am ---... Just setting gtk2 might be enough for most people but it does not always work. I just noticed that it does not work on Debian Bookwork at the moment. Sigh....


--- End quote ---

@dbannon

I just tried a qt5 app on Debian 12 and it didn't work -platformtheme gtk2

I fixed it with:

First I checked that I have gtk2 platformtheme installed for qt5:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---find /usr/lib/x86_64-linux-gnu/qt5/plugins/platformthemes -type f

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---/usr/lib/x86_64-linux-gnu/qt5/plugins/platformthemes/libqgtk3.so

Only gtk3 appears, so I have to install the qt5-gtk2-platformtheme package.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---sudo apt install qt5-gtk2-platformtheme

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---/usr/lib/x86_64-linux-gnu/qt5/plugins/platformthemes/libqgtk2.so/usr/lib/x86_64-linux-gnu/qt5/plugins/platformthemes/libqgtk3.so
And now it works

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---./my_qt5_app  -platformtheme  gtk2 <enter>
But a Warning appears:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Failed to load module "canberra-gtk-module"
I solved it with:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---sudo apt install libcanberra-gtk-module

dbannon:
Thanks RCA, but thats not a good solution in packaging terms. If you put in that the package is dependent on qt5-gtk2-platformtheme then it will install all of gtk2 into a, say, pure Qt system. All 220Meg of it.

Debian is now treating dependency on GTK2 as a bug, not a critical one but  report-able one. My plan for my app now is to add a dependency on qt5ct, its only 180K. Only on the Qt version of course. Thats the one in Debian now....

As for Canberra, yes, that been the case for a long time, I have no idea why, my app at least does not use any of the canberra api, I guess another dependency is incorrectly dependent on it.

Davo

Navigation

[0] Message Index

[#] Next page

Go to full version