从qrc文件加载.qss主题

时间:2017-05-21 00:38:00

标签: c++ qt qt5

我试图使用qdarkstyle主题。 我已经按照代码但我似乎无法加载样式表。以下是我在main.cpp中加载代码的方法:

QFile f(":qdarkstyle/style.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());
}

但是exists()总是假的。 qdarkstyle文件夹位于我的主要源

的子目录中

enter image description here

我的项目资源如下所示:enter image description here

我尝试过以下电话:

QFile f(":qdarkstyle/style.qss");
QFile f("://qdarkstyle/style.qss");
QFile f(":/qdarkstyle/style/qdarkstyle/style.qss");

但我永远存在总是假的。关于我做错了什么的任何想法

添加我的.pro文件

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MediaManagerV2
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

RESOURCES += \
    resources.qrc \
    qdarkstyle/style.qrc

链接到来源

https://drive.google.com/file/d/0BwXCsqWT3wkXV1dYNzF1dFM4dTQ/view?usp=sharing

修正

修改后的代码

QApplication a(argc, argv);
MainWindow w;
w.show();

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

基本上移动了show()以上

1 个答案:

答案 0 :(得分:1)

您已将.qrc文件添加到.pro:

RESOURCES += qdarkstyle/style.qrc

在调用样式表之前移动show()