Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically retrieve values for QSS style sheet properties

If I have a label that has a margin that is set using setMargin(), then I can use margin() to get the value.

But when if I set the padding using a style sheet?

ui->label->setStyleSheet("QLabel {padding: 0px 5px 10px 15px;}");

How can I get the values programmatically? Is there a function that will give me the value for a given property in the style sheet? Is there a function like ui->label()->styleSheet->getProperty("padding:left")?

like image 257
sashoalm Avatar asked Dec 14 '25 01:12

sashoalm


1 Answers

I've not found any way to give all properties. I think it doesn't exist for architectural reasons. But you can take most of it from QWidget substrucrutes. For example you may take background-color from palette().color(QPalette::Window). If you want to supply qss background in your QWidget with overridden paint event you can do it this way:

void PulseChart::paintEvent(QPaintEvent*)
{
  QPainter p{this};
  p.fillRect(QRect{QPoint{0,0}, this->size()}, palette().color(QPalette::Window));
}
like image 60
BOPOHOB Avatar answered Dec 15 '25 18:12

BOPOHOB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!