Nominate our 2022 Qt Champions!

QWidget setStyleSheet and text color

  • Hello,

    I try to get working veerey simple thing:
    I have class "MyTabButton : public QPushButton" and want to draw by myself with MyTabButton::paintEvent.
    I also setStyleSheet with normal & checked style:
    QPushButton {
    border: 1px solid #acacac;
    background: #e1d3c7;
    background-position: top center;
    text-align: bottom center;
    color: #89827d;
    font-size: 15pt;
    font-weight: bold;

    QPushButton:checked {
    background: #fe4e1c;
    color: #ffff00;

    But got painful problem: inside paintEvent I can't get color for checked state to draw text with:
    @style()->drawItemText(&p, lrect, Qt::AlignCenter | Qt::AlignBottom, opt.palette, true, text(), QPalette::Text);@

    I check source code - Qt use QRenderRule to draw, but this is internal class.
    How can I get colors from stylesheet in paintEvent ? Or hack somehow ?

    [edit: added missing coding tags @ SGaist]

  • Lifetime Qt Champion


    Style sheet and palettes are two independent beasts. However you could try to integrate the palette in the style sheet like described "here":

    Hope it helps

  • Thank you, didn't see.
    At least will be easier to synchronise stylesheet colors & native style colors.

Log in to reply