Can't change background color of progress bar
Maurizio Berti
maurizio.berti at gmail.com
Thu Aug 15 03:56:46 BST 2024
As the documentation about the stylesheet property
<https://doc.qt.io/qt-6/qwidget.html#styleSheet-prop> explains, that
property holds the widget's style sheet, meaning that its "setStyleSheet()"
setter always *overwrites* it.
When you call setValue() on a widget (for instance, a spin box) you
certainly do not expect it to sum that value to the current one, do you?
Similarly, calling setStyleSheet() then does *not* "update" the existing
QSS, but always applies the new given one, completely discarding and
ignoring any previous value. Besides, it's not called "updateStyleSheet".
Which obviously makes sense.
Suppose that you call it once with a complex set of rules that are applied
differently depending on the combination of those rules (for instance,
:hover and :disabled): how would you eventually update/remove/amend those
rules reliably?
That may be achievable in DOM with appropriate scripting or libraries, but
that's not the scope (or the implementation) of QSS, which only *inherit*
the concepts of the CSS syntax.
You *may* consider updating the current QSS by doing something like this:
widget.setStyleSheet(widget.styleSheet() + '/* some new style sheet */')
Still, this means that the parsing cannot directly take care of duplicate
properties, resulting in a full reparse of the whole style sheet every time
the function is called, possibly causing extreme and completely unnecessary
overhead. The QSS parser is relatively fast, but not that fast, and
shouldn't be misused; and doing something like this in sequential calls
wouldn't make any sense at all.
If you want to apply a QSS with multiple rules, you must do that within a
*single* call to setStyleSheet.
self.lista_progress[a].setStyleSheet("""
QProgressBar {
background-color: black;
}
QProgressBar::chunk {
background-color: green;
}
""")
Cheers,
MaurizioB
PS: please note that this mailing list is aimed at aspects that are
strictly involved to the PyQt library and issues specifically related to
it, not the general Qt API: your question is about Qt behavior, not that of
this Python binding. Consider posting such questions on related websites
(eg. the Qt forum) or on services such as StackOverflow, as similar
questions are often ignored here (I just answered because it's Ferragosto
e, vabbè, sticazzi ;-) ).
Il giorno gio 8 ago 2024 alle ore 12:26 Luca Bertolotti <
luca72.bertolotti at gmail.com> ha scritto:
> Hello
> This is my code:
> self.lista_progress[a].setMaximum(100)
> self.lista_progress[a].setValue(int(v))
> self.lista_progress[a].setStyleSheet("QProgressBar {background-color:
> black;}")
> self.lista_progress[a].setStyleSheet("QProgressBar::chunk
> {background-color: green;}")
>
> Where self.lista_progress is a list of progressbar , but only the line
> self.lista_progress[a].setStyleSheet("QProgressBar::chunk
> {background-color: green;}")
> work
> the line
> self.lista_progress[a].setStyleSheet("QProgressBar {background-color:
> black;}")
> don't make anything
> What is wrong
>
--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240815/76d45045/attachment.htm>
More information about the PyQt
mailing list