简体   繁体   中英

Qt Customwidget appearance not changing after setting stylesheet

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, eg, 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

I am not sure if this helps, but overriding QEvent::Paint for custom widgets is not needed in order to be able to apply style sheets to them. It might even be causing problems for you, since only QStyle::PE_Widget element is requested to be drawn while many more other elements can be drawn by Qt for you. Also, QWidget::setStyleSheet() should unpolish and polish everything automatically for you, thus no repolishing should be required. In other words, only the following lines should be required for you:

auto newStyleSheet = QString("CustomWidget{background-color:black;}");
m_previewFrame->setStyleSheet(newStylesheet);

I am not sure if it would work, but as I have tried (by setting it through a QTextEdit), it works without any problems as many times as you set stylesheet. In any case, the problem should be somewhere else. By the way, I am using C++11, Qt Creator 4.8.2, and Qt5.9.8. Maybe newer Qt version has such type of a bug? I doubt very much that C++17 or Qt Creator 4.9.2 would cause any issues.

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