Important: Please read the Qt Code of Conduct -

Undoing an added "setStyleSheet" property

    • I inherit from a class whose constructor includes someWidget.setStyleSheet("background-color: transparent;")
    • For my derived class I do not want to alter the widget's background color.
    • After the base class constructor has been called, I can't/don't want to just go someWidget.setStyleSheet("") to undo that, because for all I know there may have been something else in the setStyleSheet() call with other styles which I don't want to affect. (So I find solution unacceptable.)
    • I am used to HTML/CSS/JS, where I could access and alter an individual "backgroundColor" property, but I don't see that from Qt CSS.
    • So all I can see is:
    styleSheet = someWidget.styleSheet()
    styleSheet = styleSheet.regularExpressionReplace("background-color: ...", "")

    which is all a bit ugly.

    Is there a better way?

    The widget in this case is a QTextEdit, though really the question might be aimed at any widget type, but I suppose I'll accept whatever solution is best in this particular case if necessary.

  • Moderators

    @JNBarchan Hi, why don't you give your baseclass an object name and referece that in your stylesheet, that way in your deriufec widget you simply rename the widget

    something along the line of:

    //Base Class
    //Later in your subclass

    that should work.

  • @J.Hilk

    1. I have no desire to change the base class code, else I would have said so.
    2. For all I know, the base class might go:
      this->setStyleSheet("QWidget#BaseWidget{background-color:white; another-property: something; ...}");
      and I will want any other style properties, just not the background color, as I said.

    The question is: how to remove a single property which may have been set in a previous setStyleSheet(...) call?

    I've just realised, it's not just "remove" a single property, it's "override" a single property, which might or might not have been set. In this case, I'm willing to set background color to lightgrey instead of removing it, but it still leaves me with the issue of having to parse & change the whole of the existing string in case other properties might have been set....

  • Moderators

    @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;");


    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 all QTextEdits' default background color to something else, and initial/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:

    1. 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.

    2. 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) or QWidget::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

  • 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.



  • @Vinod-Kuntoji
    That does not just alter the background-color property, as I might want. That alters the whole of the stylesheet, and relies on knowledge that font-size has been set to 12pt 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 any background-color on the widget. However, that is by-the-by to the central point described in the paragraph above.

Log in to reply