change text color on QRadioButton
I have text for a QRadioButton like so:
feet (meters)
It should look pretty much exactly like this, except I want "meters" to be a little lighter than black, i.e. 60,60,60.
If this were a QLabel, I'd do:
Code:
QString myString
("feet (<a style=\"color:#606060\">meters</a>)");
myQLabel->setText(myString);
But it's not a QLabel. Is there a simple fix?
Re: change text color on QRadioButton
I'm not sure but maybe you could use the setPalette function?
Have you tried something using stylesheet?
Re: change text color on QRadioButton
Quote:
Originally Posted by
Corinzio
I'm not sure but maybe you could use the setPalette function?
Have you tried something using stylesheet?
That wont work because with that you can only chance the whole text color. not specific ones. I fear you have to subclass QRadioButton and do the drawing yourself, or:
use a layout out a label and a non text holding radiobutton on it, use setBuddy and have almost the same result.
Re: change text color on QRadioButton
#include <QtGui>
int main(int argc ,char** argv)
{
QApplication app(argc,argv);
QRadioButton* button = new QRadioButton();
button->setText("This is my Colored Radio Button");
button->setWindowTitle("RadioButton with Colored-Text");
QPalette* palette = new QPalette();
palette->setColor(QPalette::Foreground,Qt::red);
button->setPalette(*palette);
button->show();
return app.exec();
}
Re: change text color on QRadioButton
// For those who are interested in a solution here is some code that will do the trick. The downside to this solution as coded is that you must click on the button to toggle the state. Of course, you could connect a signal and slot for the label that would toggle the button to get it function more like a real button. Hope this helps someone.
// create a horizontal layout
QHBoxLayout *hboxLayout = new QHBoxLayout();
// set spacing to 0 --- do not leave space between widgets
hboxLayout->setSpacing(0);
// create your button
QRadioButton* button = new QRadioButton();
// no text
button->setText("");
button->show();
// add it to the layout
hboxLayout->addWidget(button);
// create your label
QLabel* label = new QLabel();
// set your text with style
label->setText("feet (<a style=\"color:#606060\">meters</a>)");
label->show();
// add it to the layout
hboxLayout->addWidget(label);
// add some stretch to force the button and lable together
// this will push them to the left
hboxLayout->addStretch(1);
// add the horizontal layout to your parent layout
// this might be a main vertical layout or another layout within the main layout
parentLayout->addLayout(hboxLayout);