Results 1 to 5 of 5

Thread: change text color on QRadioButton

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. QString myString("feet (<a style=\"color:#606060\">meters</a>)");
    2. myQLabel->setText(myString);
    To copy to clipboard, switch view to plain text mode 

    But it's not a QLabel. Is there a simple fix?

  2. #2
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    16
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: change text color on QRadioButton

    I'm not sure but maybe you could use the setPalette function?
    Have you tried something using stylesheet?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: change text color on QRadioButton

    Quote Originally Posted by Corinzio View Post
    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.

  4. The following user says thank you to Lykurg for this useful post:

    vonCZ (21st May 2009)

  5. #4
    Join Date
    Nov 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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();
    }

  6. #5
    Join Date
    Jan 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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);

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Lineedit text change signal
    By zgulser in forum Qt Tools
    Replies: 2
    Last Post: 19th January 2009, 14:38
  3. Replies: 8
    Last Post: 15th May 2007, 10:21
  4. Can't change the text display of a QTreeview item.
    By johnny_sparx in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2006, 02:03
  5. Change the text color tab page
    By gtthang in forum Qt Programming
    Replies: 4
    Last Post: 18th February 2006, 18:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.