简体   繁体   English

设置样式表后,Qt Customwidget 外观不会改变

[英]Qt Customwidget appearance not changing after setting stylesheet

I want to provide a system to write own Styesheets for my Application.我想提供一个系统来为我的应用程序编写自己的 Styesheets。 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++ )我像这样加载样式表:我使用 QFileInfo 来检查文件是否存在(就像在这篇文章中如何检查文件是否存在于 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. Edit3:m_previewframe 是一个 Qframe 对象,我想更改一个 customWidget 的样式,它是 m_previewFrame 的子项。 Do i have to polish/unpolish for every children instead for only the QFrame?我是否必须为每个孩子打磨/取消打磨,而只为 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 )样式表用于自定义小部件,因此像本文中的那样覆盖了paintEvent 自定义小部件的 Qt 样式表编辑:使用 QFile::Text 很重要( 无法使用 qss 文件设置样式表属性

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:如果我尝试像这样直接传递 QString :

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. Edit2:这绝对是一个更新问题,既不是波兰语/取消波兰语 + update() 也不是 ensurePolished() + update() 工作。
I am not sure how i can force my QFrame and its children to rerender.我不确定如何强制我的 QFrame 及其子项重新渲染。
Qt unpolish: Qt 不抛光:

Note that unpolish() will only be called if the widget is destroyed.请注意,只有在小部件被销毁时才会调用 unpolish()。 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;在某些情况下,这可能会导致问题,例如,如果您从 UI 中删除小部件,将其缓存,然后在样式更改后重新插入; some of Qt's classes cache their widgets. Qt 的一些类缓存它们的小部件。

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我正在使用 C++17、Qt Creator 4.9.2 和桌面 Qt 5.13.0 MinGW 64 位

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.我不确定这是否有帮助,但不需要覆盖自定义小部件的 QEvent::Paint 以便能够将样式表应用于它们。 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.它甚至可能会给您带来问题,因为只请求绘制 QStyle::PE_Widget 元素,而 Qt 可以为您绘制更多其他元素。 Also, QWidget::setStyleSheet() should unpolish and polish everything automatically for you, thus no repolishing should be required.此外, QWidget::setStyleSheet() 应该自动为您取消抛光和抛光所有内容,因此不需要重新抛光。 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.我不确定它是否会起作用,但正如我所尝试的(通过 QTextEdit 设置它),它在设置样式表的次数中没有任何问题。 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.顺便说一下,我使用的是 C++11、Qt Creator 4.8.2 和 Qt5.9.8。 Maybe newer Qt version has such type of a bug?也许较新的 Qt 版本有这种类型的错误? I doubt very much that C++17 or Qt Creator 4.9.2 would cause any issues.我非常怀疑 C++17 或 Qt Creator 4.9.2 会导致任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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