Solved Change text color of QRadioButton text with style setStyleSheet without changing the check mark color
-
I have a QRadioButton with the text member set to "My String":
radioButton = new QRadioButton(dialog); radioButton->setText("my string");
Then I have set the style of the dialog
dialog->setStyleSheet(style);
Now I want to change the color of "My String" so I did:
style = "QRadioButton { color: blue:}";
This gives me:
so it changes the color of the "checked" mark as well. How can I change the color of "My String" only?
-
Hi
It can depend on the platform, you are on.
Not all platforms QStyle draws the same.
On Windows only text changed.You might be able to change it via
https://doc.qt.io/qt-5/qproxystyle.html
and
https://doc.qt.io/qt-5/qproxystyle.html#drawControl
and overwrite for
CE_RadioButtonLabelto change its color.
ref
https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#1421 -
Thank you for your quick response.
I'm actually on Windows. So I'm guessing there must be some other style code that is messing up my radio buttons. -
Hi
Oh. Well, i would try with test app and just one radio and set stylesheet to it and see.
It could also be Qt version difference. Especially if using Qt6.What windows ver are you on ?
even the "hole" looks slightly differnt.
-
I'm using Windows 10 and Qt 5.15.
-
Ok. same here :) (5.15.2)
So yes, i think you have something else in play.But fast test should show that clearly.
-
I created a small test project. https://github.com/Drllap/QRadioButtonTest
Updated my Qt to 5.15.2But I still get the same behavior
Edit:
Tried on an old Qt installation I had, 5.7, and get this:
-
@Palli
Pretty odd, as with 5.15.1, i get
ill try 5.15.2 -
BTW, I'm using Visual Studio 2019 to compile
Compiling for 32 bit
Don't know if that matters. -
well i use same version.
just 64 bit.5.15.2 still showing as black.
i did notice you are not calling constrcutor for QMaindows
class MainWindow : public QMainWindow { public: MainWindow() <<<< not calling base { setStyleSheet("QRadioButton { color: red};"); setCentralWidget(new ViewField); } };
Not that i think it should matter for this issue.
Also, if you do this in a default GUI project, it still did it ?
could you run this project ?
https://www.dropbox.com/s/1ecbob3kwz59l0n/colorme.zip?dl=0 -
@Palli: The first image with the red indicator is the
Windows
style, the second one isWindowsVista
. It's clearly visible because the current style (vista) doesn't draw 3D-effects but is flat. -
@Christian-Ehrlicher
Ahh you're a hawk eye!
So somehow the WindowsVista gets loaded ? -
@mrjj WindowsVista is the correct (default) one. Don't know if this was (is?) compilable with MinGW though.
-
@Christian-Ehrlicher
But we both use the same visual studio 2019 compiler and his sample on my pc,
uses the flat version and does not show as red but on his pc with the same Qt version and same compiler uses the
"3d" one and get red dot.
So not sure how we can differ considering he is on win 10 too. -
Don't know why the windowsvista style is not loaded - it's a plugin.
-
Hi Could you please add
#include <QStyleFactory>
#include <QDebug>int main(int argc, char *argv[]) { QApplication a(argc, argv); qDebug() << QStyleFactory::keys().join(" "); MainWindow mw; mw.show(); return a.exec(); }
to your 5.15.2 project and see what it lists.
for me its
"windowsvista Windows Fusion" -
@mrjj When I print out the keys from the style factory I get this:
Ran your Colorme and get this:
I use cmake to generate visual studio solution file. Maybe that is the root cause of my issues?@Christian-Ehrlicher
I don't know if it is relevant but I manually copy the .dlls I need the run the executables:
Since there windows vista style is not being loaded I wonder if there a .dll I missed? -
@Palli said in Change text color of QRadioButton text with style setStyleSheet without changing the check mark color:
I manually copy the .dlls
That might be the issue, yes. windeployqt also copies style dlls which I think are missing here.
-
Thank you so much for your help, I have solved this but would definitely not been able to without your help.
The problem was that the .dll with the windowsvistat styles were not being loaded. Added styles/qwindowsvistastyle.dll to my build folder and it has fixed this issue.
-
Well I completely missed it was when running standalone :)
Happy you found it :)=