3

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")?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • why can't you store the values somewhere prior using them ? – Frederik.L Jun 25 '14 at 06:03
  • 1
    @Frederik.L Because they are entered by the user at run-time. In any event, I'm asking how to do it, not whether I should do it, so my question is valid regardless of my motives to do it. – sashoalm Jun 25 '14 at 06:10
  • I was only asking because it would have saved you from some extra pain if it had been possible to change the approach. Getting specific properties from stylesheets in Qt is more likely a messy swamp with regex. – Frederik.L Jun 25 '14 at 06:34
  • 1
    @Frederik.L Yes, I found this - https://qt-project.org/forums/viewthread/4656, they say it's not possible unless you parse it yourself. – sashoalm Jun 25 '14 at 06:39
  • Actually, someone has already asked this question - http://stackoverflow.com/questions/4434604/determining-qt-stylesheet-options-programmatically, but it was worded differently, that's why I didn't find it before asking. – sashoalm Jun 25 '14 at 06:45
  • @sashoalm There's no getter for it, unless you create your own. After all it's CSS like format, lots of works are involoved in dealing with strings. Perhaps you can take a look at Qt's **stylesheet** example to see how they let user choose different stylesheet in the runtime. – Tay2510 Jun 25 '14 at 06:46
  • @sashoalm If you really need to manage stylesheets, I would recommend a clean layer between the user and your app with something like this: http://www.netsurf-browser.org/projects/libcss/ – Frederik.L Jun 25 '14 at 06:50
  • @Frederik.L I dont' it. Qt already has the ability to parse and manage stylesheet files, why should OP need that? – Tay2510 Jun 25 '14 at 06:53
  • @Tay2510 You never want to rely on regex to parse either html, css or any other very permissive text to validate entries unless this is something very bullet proof. Take a look at this explanation (assume that HTML is replaced with CSS) : http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Frederik.L Jun 25 '14 at 06:58

1 Answers1

1

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));
}
BOPOHOB
  • 525
  • 1
  • 4
  • 13