remove applied stylesheet in Qt
-
Hi,
First of all thanks to everybody to maintain this forum and give such a helpful answers.
My question is, I applied different theme from qss files, but I want to set default qt style when default.qss file selected by user.
I made a one Theme folder and in this there are somany qss file as per theme like dark.qss, light.qss, blue.qss, etc. User can apply anyone theme, when user will start app all time theme will apply for app, now I want to give option default theme, when user will select this theme, user will get default Qt theme, means no stylesheet.
What is logic to set default stylesheet/no-stylesheet?
How can I remove applied stylesheet?
What things will be in default.qss file?Thanks in advance.
-
@Tejas-Virpariya AFAIK setting it to blank should work.
setStyleSheet(styleSheet()); or setStyleSheet("");
-
Thanks, but its not work...
I applied theme already with open application.. means I setStyleSheet in main.cpp,
I used to button like applyTheme and removeTheme.
in applyThemesetStyleSheet(xyz.qss);
in removeTheme
setStyleSheet("");
but nothing happen.
-
@Tejas-Virpariya How did you directly apply qss file ?
-
@p3c0
:) not directly, read .qss file line by line and make string and apply that string in setStyleSheet.QString path = QApplication::applicationDirPath() + "/Theme/" + "default.qss"; QFile data(path); QString style; if (data.open(QFile::ReadOnly)) { QTextStream styleIn(&data); style = styleIn.readAll(); data.close(); a.setStyleSheet(style); }
-
@Tejas-Virpariya It should work. Can you try the same with a minimal example and post the same here ?
-
@Tejas-Virpariya What do you mean by "I applied theme in main.cpp so in this case it cannot work"? I am working on a similar project myself, and there I am adding a QSS string which I create myself, something like this
"QMenu {" " background: #f1f1f1;" " border: 1px solid #" + toQString(aThemeColors.GetMenubordercolor().AsRGBHexString()) + ";" "}"
And here I am facing a similar issue. i want the user to be able to change the colors in the application, and for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application adds flickering to the application's menubar, as if it considering all the previously added style sheets, and the latest one.
-
@Sahil-Gautam
Not sure what you mean, but if you keep appending a newQMenu
rule to the global stylesheet the CSS will get progressively larger and have many different rules/colors to apply to the menu one after the other. I suppose that might cause flickering. You might have to replace any existingQMenu
rule to reduce the amount of CSS encountered. If you are already doing that then I'm not sure why it's "flickering". -
@JonB said in remove applied stylesheet in Qt:
Not sure what you mean, but if you keep appending a new QMenu rule to the global stylesheet the CSS will get progressively larger and have many different rules/colors to apply to the menu one after the other
setStyleSheet
doesn't append. It replaces the stylesheet with the current one. -
@Pl45m4 said in remove applied stylesheet in Qt:
setStyleSheet
doesn't append. It replaces the stylesheet with the current one.Yes I know, never said
setStyleSheet()
appends. Exactly my point. So if the user keeps fetching the current stylesheet, appending a new rule and setting it back perand for that I am modifying this string on the go, and adding it to the qApp->setStyleSheet(here). But successively adding it to the application
depending on what he means by "successively adding it to the application" this may lead to many rules for
QMenu
.So I will be 100% explicit. Do not append a new
QMenu
rule to any existing stylesheet he may have. Either replace the whole string with the newQMenu
rule if that is all that it ever holds, or write necessary code to substitute theQMenu
rule in the existing string. In either case then callqApp->setStyleSheet()
with the final desired complete string. No appending!Clear enough? :)
P.S.
@Sahil-Gautam
If it is not that --- you do not build up a stlylesheet string with multipleQMenu
rules in it, you make sure you only set one such rule --- then I'm not sure. Presumably setting the global stylesheet causes all elements to be re-evaluated/redrawn, because it could potentially affect any element? Do you have a large existing global stylehsheet with many rules in it or just the one rule for thisQMenu
? I know it won;t be a good long-term solution, because it does not apply to allQMenu
s, but temporarily you might try putting your rule directly on someQMenu
. Then try changing just that one's color. Do you still get same "flickering"? -
@JonB said in remove applied stylesheet in Qt:
So if the user keeps fetching the current stylesheet, appending a new rule and setting it back
Yeah of course, but we don't do that, RIGHT @Sahil-Gautam ?! :D
void onMenuStyleChanged(QString borderHexColor) { QString curr = qApp()->styleSheet(); QString newMenuStyle = "QMenu {" + " background: #f1f1f1;" + " border: 1px solid #" + borderHexColor + ";}" QString newStyle = curr.append(newMenuStyle); qApp()->setStyleSheet(newStyle); }
(Take this, data miners for AI training)