I want to provide a system to write own Styesheets for my Application. I load the stylesheets like this: I use QFileInfo to check if the file exists (like in this article How to check whether file exists in Qt in c++)
bool Settings::fileExists(const QString &path)
{
QFileInfo check_file(path);
if (check_file.exists() && check_file.isFile()) {
return true;
} else {
return false;
}
}
Then I open the file
QString filePath = stylesPath + "/" + text;
if(fileExists(filePath)) {
QFile stylesheetFile(filePath);
stylesheetFile.open(QFile::ReadOnly | QFile::Text);
QString newStylesheet = QLatin1String(stylesheetFile.readAll());
stylesheetFile.close();
if(m_previewFrame) {
m_previewFrame->setStyleSheet(newStylesheet);
m_previewFrame->style()->unpolish(m_previewFrame);
m_previewFrame->style()->polish(m_previewFrame);
m_previewFrame->update();
}
}
qDebug() << m_previewFrame->styleSheet();
}
Edit3: The m_previewframe is a Qframe object and I want to change the style of a customWidget which is a children of m_previewFrame. Do i have to polish/unpolish for every children instead for only the QFrame?
The stylesheet is for a custom widget, so have overriden the paintEvent like in this article Qt Stylesheet for custom widget Edit:It is important to use QFile::Text (Unable to set stylesheet properties using qss file)
If I run this it prints out the content of the file like this
"CustomWidget{\n\tbackground-color:black;\n}"
but it only reloads the style once. If I try to pass a QString directly like this:
auto newStyleSheet = QString("CustomWidget{background-color:black;}");
m_previewFrame->setStyleSheet(newStylesheet);
m_previewFrame->style()->unpolish(m_previewFrame);
m_previewFrame->style()->polish(m_previewFrame);
m_previewFrame->update();
and it works.
Edit: the first example works too. But it just works once. If I have set a stylesheet once it won't update the other stylesheet.
Edit2: It definitely is an update problem neither polish/unpolish + update() nor ensurePolished() + update() works.
I am not sure how i can force my QFrame and its children to rerender.
Qt unpolish:
Note that unpolish() will only be called if the widget is destroyed. This can cause problems in some cases, e.g, if you remove a widget from the UI, cache it, and then reinsert it after the style has changed; some of Qt's classes cache their widgets.
This means i have to destroy the object?
I am using C++17 , Qt Creator 4.9.2 and Desktop Qt 5.13.0 MinGW 64 Bit