Important: Please read the Qt Code of Conduct -

stylesheet in form file

  • Hi guys,

    We have set the background color of a single widget in one of the dialogs' form files to white.

    We use global stylesheets that are applied from qsettings on start of the application or on choosing from a settings dialog and clicking a pushbutton.

    Now we have the problem that the style that was set in the form file will not be changed by this global stylesheet.
    So for example every widget has black background except for this single white one.

    How would I achieve that it can be changed from the global stylesheet?

    Or do I have to make a "default" global stylesheet that only contains those special widgets?


  • Lifetime Qt Champion

    In a style sheet you can use both names and types
    So i would use one global stylesheet and target the special widgets by type or by name.

    setStyleSheet("QLineEdit#nameEdit { background-color: yellow }");

    only the QLineEdit called nameEdit will be affected.

  • @mrjj

    We tried that, but the style set in form files is applied on executing the dialog and overwrites the global stylesheet.
    If that makes sense.

  • Lifetime Qt Champion

    It only makes sense if style sheet is set on application level and not on the dialog.
    As far as i know they are not mixable and a given widget will only be affected by one
    style sheet at a given time. So if you set a local stylesheet in dialog, the global is only
    in effect "outside".

  • @mrjj okay thx.

    so what would be the easiest way to settle this?
    use default global with those few changes?

    Someone told me to use properties instead.
    I haven't looked into that yet though

  • Your best bet really is to remove the stylesheet from the forms entirely and go with a global stylesheet, in my opinion.

    If you are using the stylesheets as a 'skin' type option then simply provide a default one to be used if a custom one isn't selected by the end user.

  • @Swerved yeah that's what i use now.
    It's only a few lines anyway so I can just add it in my slot function without extra default qss.
    It's just a bit sad that I can't see it directly in form.

    thx this is resolved for my needs.

    would be nice to have better ways though. Maybe !important should be added sometime

Log in to reply