Unsolved Undoing an added "setStyleSheet" property
-
@JNBarchan Mhh,than I don't see an other way than that what you described in the OP, or something very similar.
Maybe someone else has a better idea.
-
I don't know if the CSS 'initial' and/or 'unset' values work with Qt stylesheets, might be worth a try? For example:
setStyleSheet("background-color: initial;");
or
setStyleSheet("background-color: unset;");
-
@jazzycamel
This looked good, and is a reasonable answer to the question!However, in my case both of these resulted in the
QTextEdit
's going black, which is not good given that the text on it is black too :) I'm guessing that somewhere else (like the app's global stylesheet which the code reads in and applies) there is some default entry which sets allQTextEdit
s' default background color to something else, andinitial
/unset
reverts to some other base, default color, losing that :(So a possible good answer, but in my case it really does look like I need to either remove or alter the
background-color
(only) explicitly on the widget...? -
Thats a shame, maybe consider raising a feature request for that one.
Seems to me that saving and restoring the original stylesheet might be the only way to then.
-
@jazzycamel
Yep, at present that seems to be the only route.setStyleSheet
principle could do with a couple of enhancements:-
appendToStyleSheet(str)
. Adds some more stuff to the existing stylesheet without requiring you to fetch its current content and then reset the whole thing. Having said that, there are calls to fetch->change->settonew, so it's only really a convenience. -
From a
setStyleSheet
, I assume that at some level Qt parses the properties string into its constituent properties to set. Make it so the individual properties can then be accessed directly, e.g.QWidget::setStyleProperty("background-color", newValue)
orQWidget::removeStyleProperty("background-color")
.
At the moment, because there's only a
setStyleSheet
, the code I have inherited is full of code in different modules using this, and who knows whether that will overwrite other deliberate changes from elsewhere. It doesn't lend itself to safe, self-contained programming features. -
-
Chances of this happening, apart from a contribution to Qt, are pretty slim though. The roadmap is currently fascinated with QML, customising QtWidgets is on the wayside.
-
@JNBarchan ,
To undo the stylesheet, you can use
textEdit->setStylesheet(""); -
That would undo the whole stylesheet - not what the topic is looking for. OP is interested in undoing 1 property.
-
@Vinod-Kuntoji
As @Vadi2 has just said, the whole point of the question is how to selectively modify certain individual property assignments which may have been made previously, without altering any others. -
@Vadi2 said in Undoing an added "setStyleSheet" property:
Chances of this happening, apart from a contribution to Qt, are pretty slim though. The roadmap is currently fascinated with QML, customising QtWidgets is on the wayside.
Although I am a total noob to Qt, I read yesterday (somewhere...) that actually the roadmap now is to sideline development on QML, because it has received so much attention, and concentrate on new features for Widgets, to bring them back up to speed!
-
@Vadi2 ,@JNBarchan
Ya, I can undo only the 1 property without altering others. I hope you got the solution.
ui->textEdit->setStyleSheet("background-color:red;font-size:12pt;");
ui->textEdit->setStyleSheet("background-color:none;font-size:12pt;");
-
@Vinod-Kuntoji
That does not just alter thebackground-color
property, as I might want. That alters the whole of the stylesheet, and relies on knowledge thatfont-size
has been set to12pt
to set that too, information which is not available to me in the generic case I have described (e.g. some other piece of code somewhere has set it, which I would not be aware of). If you read what I have asked you will see this is explained, as others understand.Furthermore (I think),
background-color:none
does not "undo"background-color:red
: it explicitly sets it to "none", which will override the color from elsewhere, whereas in this case what I wish to do is remove the explicit specification of anybackground-color
on the widget. However, that is by-the-by to the central point described in the paragraph above. -