简体   繁体   English

QT5:无法使用 qss 文件设置样式表属性

[英]QT5: Unable to set stylesheet properties using qss file

I'm currently working on an app and I'm unable to set global stylesheet.我目前正在开发一个应用程序,但无法设置全局样式表。 Here's the code I've been trying:这是我一直在尝试的代码:

QFile file(":/dark.qss");
        file.open(QFile::ReadOnly | QFile::Text);
        QTextStream stream(&file);
        qApp->setStyleSheet(stream.readAll()); '''

and

 QFile f(":/dark.qss");

if (!f.exists())   {
    printf("Unable to set stylesheet, file not found\n");
}
else   {
    f.open(QFile::ReadOnly | QFile::Text);
    QTextStream ts(&f);
    qApp->setStyleSheet(ts.readAll());
}

For the first one I'm getting: QIODevice::read \dark.qss device not open对于我得到的第一个: QIODevice::read \dark.qss device not open

and for the second one Unable to set stylesheet, file not found第二个无法设置样式表,找不到文件

The second one used to work fine 2 weeks ago (build with msys2, mingw64, qt5.14.1.3), unfortunately, I had to reinstall mysys2 and since then this is not working anymore.第二个在 2 周前可以正常工作(使用 msys2、mingw64、qt5.14.1.3 构建),不幸的是,我不得不重新安装 mysys2,从那以后它就不再工作了。

I tried to build using msys2 with all the packages I used 2 weeks ago (had to downgrade about 80 packages manually ) also straight from qt creator with no luck.我尝试使用 msys2 构建我两周前使用的所有软件包(不得不手动降级大约 80 个软件包),也直接来自 qt 创建者,但没有运气。

Any idea what might be the problem?知道可能是什么问题吗?

Maybe your.pro file is missing RESOURCES variable with.qrc file name.也许 your.pro 文件缺少带有 .qrc 文件名的 RESOURCES 变量。

You can also try to execute Clean and Run qmake on your project before building.您也可以在构建之前尝试在您的项目上执行Clean and Run qmake

The issue has nothing to do with msys.这个问题与msys无关。 The most obvious reason is an incorrect project setup.最明显的原因是项目设置不正确。 Please make sure dark.qss is added into the resource (qrc) file under "/" prefix and the qrc file is added into RESOURCES in the pro file.请确保将dark.qss添加到“/”前缀下的资源(qrc)文件中,并将qrc文件添加到pro文件中的RESOURCES中。

Also, there is a bug in qmake not adding newly added resources into the app sometimes.此外,qmake 中存在一个错误,有时不会将新添加的资源添加到应用程序中。 A full rebuild might help in this situation.在这种情况下,完全重建可能会有所帮助。

It would be great if you paste both the qrc and pro file here.如果您将 qrc 和 pro 文件都粘贴在这里,那就太好了。 A full build log could be very helpful as well.完整的构建日志也很有帮助。

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

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