Important: Please read the Qt Code of Conduct -

Resource problem with cmake

  • Hi everyone,

    I am building a desktop app with Qt 5.3.1 and cmake 3, and have some troubles with the resources. I have a .qss file which works fine when loading directly from main.cpp, but when it comes to using resources, it does not work.

    I have one .qrc file in the same folder as the main.cpp, and added the .qss file in the qrc , my code is the following:

    <qresource prefix="/">

    My code in main.cpp :

    @ QFile File(":/ScStyleSheet.qss"); | QIODevice::Text);
    QString StyleSheet = QLatin1String(File.readAll());


    When I want to use this resource, I get an error: "QIODevice::read: device not open"

    In my cmakelists.txt, the relevant section is:

    @set (project_RESOURCES ScRes.qrc )

    QT5_WRAP_CPP (project_HEADERS_MOC ${project_HEADERS})
    QT5_WRAP_UI (project_FORMS_HEADERS ${project_FORMS})

    set_source_files_properties(${project_RESOURCES_RCC} PROPERTIES GENERATED ON)

    ADD_EXECUTABLE (Scanopus ${project_SOURCES} ${project_FORMS_HEADERS} ${project_HEADERS_MOC} ${project_RESOURCES})@

    The cmakelists.txt is also in the same folder with the .qrc and the main.cpp files.

    Does anyone have any suggestions on why my resource does not load correctly?

    Thank you in advance,

  • Moderators

    Hi, welcome to devnet.

    You should check your return codes. You won't be able to read the file if QFile::open returns false.

    Anyway, with that resource file structure you should adjust your path like this:
    @QFile File(":/Resources/ScStyleSheet.qss");@

  • Hi Chris,
    Thanks for your response. Actually, the other way doesn't work either. I get a "device not open" message. I have the qrc_ScRes.cpp generated but I cannot open the .qss from the resource. File.errorString() returns "Unknown error", and File.error() returns OpenError (0n5). Does this say something to you?


Log in to reply