Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How change text color in paintEvent() method in subclass of QPushButton?
Forum Updated to NodeBB v4.3 + New Features

How change text color in paintEvent() method in subclass of QPushButton?

Scheduled Pinned Locked Moved Solved General and Desktop
1 Posts 1 Posters 787 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hi,

    Like in a topic - How change text color in paintEvent() method in subclass of QPushButton?

    I tried:

    void myButton::paintEvent(QPaintEvent *)
    {
        QStyleOptionButton option;
        option.initFrom(this);
    
        option.palette.setColor(QPalette::Text,QColor(Qt::red));
        option.text = "randomText";
    
        QPainter painter(this);
        style()->drawControl(QStyle::CE_PushButton, &option, &painter, this);
    }
    

    And I can change button's text to "randomText", but I can't change his color ( I tried change his background too ). Of course I can use styleEsheet, but I want to use only QStyleOptionButton.

    EDIT:
    I don't understand that information in QStyleOption doc ( QPalette QStyleOption::palette ):

    This variable holds the palette that should be used when painting the control
    

    I change option.palette like this:

    option.palette.setColor(QPalette::Text,QColor(Qt::red));
    

    so why the control isn't painted using this palette settings like color?

    EDIT2:
    I find solution... It was very easy :D

    option.palette.setColor(QPalette::ButtonText, QColor(Qt::red));
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved