简体   繁体   中英

Forcing a recalcuation of Qt Stylesheet appears to do nothing

Using stylesheets I'm trying to change a window's style, including all children.

All the children are named, using setObjectName and the stylesheet uses the same name to change the style of the named widgets (as well as a few extract that never needs to be changed). I'm recalculating the stylesheet by first inserting values into an std::map , mapping names, such as QWidget#hello!hover , to a set of key-value pairs, such as background-color to #0f0f0f . Ie ["QWidget#hello!hover"]["background-color"] = "#0f0f0f" , would be one entry.

When the stylesheet map is changed I recalculate the entire stylesheet and set it using setStyleSheet on the main widget, adding /* */ at the top (supposedly, this should force a recalculation of the stylesheet). However, this doesn't appear to do anything at all.

Doing this, in the constructor of the main widget works fine, but doesn't do anything once it's been set at first.

The stylesheet I calculate are valid and all the entries are ordered in the way I expect it to, all widgets have the expected names and since it works the first time I'm setting it, I suspect the problem is that either the new stylesheet has no effect or it simply has no effect on the children.

I'm using Qt 5.4 (old shitty crap software that's originally developed for Qt3) on Windows 10.

How can I fix this?

I fixed the problem by setting the stylesheet on all children of the main widget. After calculating the new stylesheet, and storing it in a QString , named stylesheet , I used:

QList<QWidget*> children = findChildren<QWidget*>(); 
for (QList<QWidget*>::iterator child = children.begin(); child != children.end(); ++child);
    (*child)->setStyleSheet(stylesheet);

and everything worked like a charm. The changes are very rare and there aren't a whole lot of stuff in the window, so the cost of doing this isn't that big of a deal -- I'm sure it can be done more effeciently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM