Qt application is not loading your custom resources from resources.qrc
-
Hello Qt Community,
I'm new to Qt and working on a small game in Qt6 (C++). I'm trying to use the resources.qrc to load icon(a Window Icon and a Icon on button), but only the built-in Qt resources are available. My custom images (flag.png, minesweeper.png) are not loading.
Project Setup
- Qt Version: 6.8.1
- OS: Ubuntu Linux
- Build System: CMake
- IDE: CLion
Project structure
Minesweeper/ │── main.cpp │── minesweeper.cpp │── minesweeper.h │── cellbutton.cpp │── cellbutton.h │── resources.qrc │── icons/ │ ├── flag.png │ ├── minesweeper.png │── CMakeLists.txt
resources.qrc
<RCC> <qresource prefix="/"> <file>icons/flag.png</file> <file>icons/minesweeper.png</file> </qresource> </RCC>
CMakeLists.txt
cmake_minimum_required(VERSION 3.30) project(Minesweeper) set(CMAKE_PREFIX_PATH "/home/panyilun/Qt/6.8.1/gcc_64/lib/cmake/Qt6") set(CMAKE_CXX_STANDARD 20) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) find_package(Qt6 COMPONENTS Core Gui Widgets REQUIRED) qt_add_resources(Minesweeper "resources.qrc") add_executable(Minesweeper main.cpp resources.cpp minesweeper.cpp cellbutton.cpp) target_link_libraries(Minesweeper Qt::Core Qt::Gui Qt::Widgets)
main.cpp
#include <QApplication> #include "minesweeper.h" #include <QDirIterator> #include <QDebug> void listQtResources() { QDirIterator it(":", QDirIterator::Subdirectories); while (it.hasNext()) qDebug() << it.next(); } int main(int argc, char* argv[]) { QApplication app(argc, argv); Minesweeper window; window.setWindowIcon(QIcon(":/icons/minesweeper.png")); window.setWindowTitle("Minesweeper"); window.show(); return app.exec(); }
minesweeper.cpp
void Minesweeper::toggleFlag(int row, int col) { auto& cell = grid[row][col]; auto* button = buttons[row * COLS + col]; QIcon flagIcon(":/icons/flag.png"); if (!cell.isFlagged && !cell.isQuestioned) { cell.isFlagged = true; cell.isQuestioned = false; button->setText(""); button->setIcon(flagIcon); } else if (cell.isFlagged) { cell.isFlagged = false; cell.isQuestioned = true; button->setIcon(QIcon()); button->setText("?"); } else if (cell.isQuestioned) { cell.isFlagged = false; cell.isQuestioned = false; button->setIcon(QIcon()); button->setText(""); } }
Any help would be greatly appreciated!
-
While this does not answer your question there is an alternative to .qrc files:
using qt_add_resources like this:qt_add_resources(Minesweeper "resources" PREFIX "/" FILES icons/flag.png icons/minesweeper.png )
Other option is mentioned here:
"if you have set(CMAKE_AUTORCC ON) then there's no need to call qt5_add_resources just add your .qrc files to the list of sources for the target as they were .cpp files"
https://forum.qt.io/post/468643 by @VRonin -
@panyilun3 said in Qt application is not loading your custom resources from resources.qrc:
QIcon flagIcon(":/icons/flag.png");
It has to be
QIcon flagIcon(":/flag.png");
We need to fix https://doc.qt.io/qt-6/resources.html#qt-resource-collection-file-qrc though... :(
-
-
@Christian-Ehrlicher Thanks Christian!