Suddenly the linked images in the qrc file are no longer visible, I don't know what happened.
I've already tried deleting the cmake generated files and recompiling, but it still doesn't work...
QRC FILE
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>sprites/peggle_title.png</file>
<file>icons/peggle.ico</file>
<file>sprites/krita.png</file>
<file>sprites/ball.png</file>
<file>sprites/peggles.png</file>
<file>sprites/stage_elements.png</file>
<file>sprites/Peggle_Fonts.png</file>
<file>sprites/sprites.png</file>
<file>sprites/scaricati/Peggle2PegglePS3LeftoverGraphic5.png</file>
<file>sprites/choose_character.png</file>
<file>sprites/mainMenu.png</file>
<file>sprites/gameMode.png</file>
<file>sprites/gameMode_buttons.png</file>
<file>sprites/character_buttons.png</file>
<file>sprites/select_single_character.png</file>
<file>sprites/action_buttons.png</file>
<file>sprites/character_face.png</file>
<file>sprites/select_difficulty.png</file>
<file>sprites/results.png</file>
<file>sprites/results_label.png</file>
<file>sounds/peghit.wav</file>
</qresource>
</RCC>
CMAKELISTS.txt
project(Peggle)
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
file(GLOB my_sources *.h *.cpp *.qrc)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOMOC ON)
add_executable(Peggle ${my_sources} resources.qrc)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Multimedia REQUIRED)
find_package(box2d REQUIRED)
include_directories(Box2D)
target_link_libraries(Peggle Qt5::Widgets Qt5::Multimedia box2d)
And for example:
QPixmap *title = new QPixmap(":/sprites/peggle_title.png");
(I'm using Visual Studio 2019 and Cmake gui)
Thanks for the help!
Most of the times, users forget to run qmake. Have you tried that? Just right clicked on the project, and select "Run qmake". If yes then try deleting all of the resource files and create new resource file. Also add the qrc path in the application's pro file.
Related
CMake file will not locate my QtCharts module, but QtCharts is, in theory, installed - here's a proof of it (that's what I get when I open the maintenance tool)
Now, I've been struggling for days to locate them with no success, I will show you my CMakeLists:
cmake_minimum_required(VERSION 3.17)
project(CPP)
set(CMAKE_CXX_STANDARD 14)
include_directories(Console)
include_directories(Domain)
include_directories(Exception)
include_directories(Repository)
include_directories(Service)
include_directories(StartingWidget)
include_directories(Tests)
include_directories(Validator)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_PREFIX_PATH "C:\\Qt\\5.15.2\\mingw81_64\\lib\\cmake")
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets Charts)
set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Charts Qt5::Gui Qt5::Widgets)
add_executable(CPP main.cpp Domain/Tutorial.cpp Domain/Tutorial.h Repository/Repository.h Service/Service.cpp Service/Service.h Console/Administrator_Console.cpp Console/Administrator_Console.h Validator/Validator.cpp Validator/Validator.h Exception/Exception.cpp Exception/Exception.h Console/User_Console.cpp Console/User_Console.h Tests/RunTests.cpp Tests/RunTests.h Repository/FileRepository.h Repository/CSVRepository.h Repository/CSVRepository.cpp Repository/HTMLRepository.h Repository/HTMLRepository.cpp Console/GUI_Console.cpp Console/GUI_Console.h Console/GUI_User_Console.cpp Console/GUI_User_Console.h StartingWidget/StartingWidget.cpp StartingWidget/StartingWidget.h)
if (NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif ()
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
and the error I get is:
CMake Error at C:/Qt/5.15.2/mingw81_64/lib/cmake/Qt5/Qt5Config.cmake:28 (find_package):
Could not find a package configuration file provided by "Qt5Charts" with
any of the following names:
Qt5ChartsConfig.cmake
qt5charts-config.cmake
Add the installation prefix of "Qt5Charts" to CMAKE_PREFIX_PATH or set
"Qt5Charts_DIR" to a directory containing one of the above files. If
"Qt5Charts" provides a separate development package or SDK, be sure it has
been installed.
Call Stack (most recent call first):
CMakeLists.txt:33 (find_package)
This is not the first time I get this error, because I had before another CMakeLists which just had include_package(Qt5::Charts) and I would get the same error. I am using Qt 5.15.2, open source (community).
I needed to reinstall the QT and use a custom install instead of using the basic 5.12.5 one, because Maintenance tool might be bugged - if someone runs into this, take care and do a reinstall
I have a Visual Studio Qt solution that I am moving to Cmake.
Everything is compiling and working fine, except all icons (.png) that I have in *.qrc file are not displayed at all.
My CMakeLists.txt is standard for a Qt project:
...
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(SOURCE_FILES ...)
set(HEADER_FILES ...)
set(UI_FILES ...)
set(RESOURCE_FILES resources/res.qrc)
add_executable(project WIN32 ${SOURCE_FILES} ${HEADER_FILES} ${UI_FILES} ${RESOURCE_FILES})
find_package(Qt5 REQUIRED COMPONENTS ...)
target_link_libraries(project PRIVATE Qt5::Core etc...)
I think that all icons are in fact embedded into .exe, because:
1. If I remove all paths from the qrc file and recompile, the .exe size is decreased by the icons size
2. I checked the qrc_*.cpp generated by AUTORCC and all the images are there.
But when I am iterating over all my resources using this code snippet
QDirIterator it(":", QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << it.next();
}
the icons are NOT there.
I have tried an alternative approach with qt5_add_resources() instead of AUTORCC and the result is the same.
I also have all the iconengines, imageformats, platforms, styles folders on the same path as .exe.
Edit:
Added the whole Cmake part of the project to github.
Considering the discussion we had in your post comment, CMake works as expected and the resources are loaded into your application.
As you mentioned "When I run my app from clion it shows the images. If I copy the exe to a folder with *.dll etc it doesn't.", so either:
Some files are missing when you copy them, you should try to use windeployqt to correctly copy what's needed at runtime
Or the problem comes from the way the qrc is neing loaded, then, I suggest that you debug the content of qrc_*.cpp files (your can debug it after you copied it outside clion) and see why it fails to load the resource
Anyway this looks more like a deployment issue than a configuration/compilation issue.
I am trying to use the CMAKE_AUTOMOC property to automatically find and compile mocable files.
However, the command set( CMAKE_AUTOMOC ON ) also includes the generated _automoc.cpp file in the Visual Studio "Source Files" filter. This is a problem for two reasons :
It creates the filter even if it was not used before, and therefore pollutes VS explorer.
It adds an additionnal file that should not be manually modified to the solution, in the middle of other source files.
I would like to know if it possible to :
1) Prevent CMake from including this file to the Visual Studio filters. I searched and found https://cmake.org/Bug/print_bug_page.php?bug_id=13788.
However using
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY AUTOMOC_FOLDER automoc)
did not change anything to my problem.
2) Remove a given entry from the .vcxproj.filters file using CMake, using a command similar to
source_group( "Source Files" FILES "filepath" )
which is used to add the entry "filepath" to the "Source Files" filter.
I am currently using CMake 3.5, VS 2015 and Qt 5.6. Here is a shortened version of the CMake that reproduces the problem :
project( myproj )
# Some stuff to include Qt libraries
# ...
set( CMAKE_AUTOMOC ON )
# These 2 lines don't change anything
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY AUTOMOC_FOLDER automoc)
# Create project
add_executable( ${PROJECT_NAME} "main.cpp" )
In the VS filter named "Source Files", I can see main.cpp and myproj_automoc.cpp, which does not even exist before the first compilation (trying to open it with VS sends an error "Cannot open the file"). In myproj.vcxproj.filters there is an entry :
Include="C:\pathto\build\myproj_automoc.cpp">
<Filter>Source Files</Filter>
which shouldn't be here since I did not ask for it.
Am I missing something ?
Thank you for your help!
I've had trouble getting this to work as documented as well. It looks like they renamed the variable in one of the releases. As of Cmake 3.0.2, you can do the following:
cmake_minimum_required(VERSION 3.0.2)
project(MyProj CXX)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY AUTOGEN_TARGETS_FOLDER MyAutoMocFolder)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
add_executable(${PROJECT_NAME}
${MyProj_HEADERS}
${MyProj_SRCS}
${MyProj_QRC}
${MyProj_UI})
Note that you have to use set_property and the property name is now AUTOGEN_TARGETS_FOLDER.
In Xcode, this puts the generated _automoc folders in the "MyAutoMocFolder" instead of littering the parent folders with them. In Visual Studio the automoc folders in the folder as well.
It doesn't however hide the project_automoc.cpp files that are generated. To move those you have to define a source group, as Armand pointed out:
source_group( MyAutoMocFolder FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_automoc.cpp )
As of CMake 3.9, you can use AUTOGEN_SOURCE_GROUP to filter MOC files.
set(CMAKE_AUTOMOC ON)
set_property(GLOBAL PROPERTY AUTOGEN_SOURCE_GROUP "Generated Files")
I would like to compile qml application using cmake.
Here is my cmake file:
cmake_minimum_required(VERSION 3.3)
project(QMLTest)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Quick REQUIRED)
add_executable(QMLTest main.cpp)
include_directories(${Qt5Widgets_INCLUDES})
add_definitions(${Qt5Widgets_DEFINITIONS})
qt5_use_modules(QMLTest Widgets Quick )
And main.cpp:
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char** argv)
{
QGuiApplication app(argc, argv);
QQuickView view;
view.resize(800, 480);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl::fromLocalFile("gui/MainWidnow.qml"));
view.show();
return app.exec();
}
When I try to compile it I get an error:
gui/MainWidnow.qml: File not found.
How can I copy all my qt resources into application directory?
Instead of copying your QML files into the application directory, I would instead recommend using Qt's resource system (1), which allows you to embed resources such as QML files directly in your executable. To do this, you need to first create a resource file such as resources.qrc to register your application resources:
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file>gui/MainWindow.qml</file>
</qresource>
</RCC>
You then add the resource file to your CMake configuration. This will create a source file from the resource file which you need to include in your application sources:
qt5_add_resources(RCC_SOURCES resources.qrc)
add_executable(QMLTest ${RCC_SOURCES} main.cpp)
Once this is done, you can use a file URL in the format :/gui/MainWindow.qml to refer to this resource, which is now embedded in your application binary. Qt will automatically resolve it for you:
view.setSource(QUrl(":/gui/MainWindow.qml"));
There are some more details in the Qt documentation on deploying QML applications (2).
I would like to extend the accepted answer.
I would write this as a comment but I don't have the reputation.
It is possible to use Qt independent CMAKE function calls instead of qt5_add_resources():
set(CMAKE_AUTORCC ON)
add_executable(myexe main.cpp resource_file.qrc)
And then you can refer to the files using :/file_relative_to_qrc
There are multiple ways to do that, for example:
add_custom_target(copy-runtime ALL
COMMAND cmake -E copy ${CMAKE_SOURCE_DIR}/gui/MainWindow.qml
${CMAKE_BINARY_DIR}
DEPENDS QMLTest)
I didn't test that, but you'll get the idea. Another option would be e.g. to use CMake's install command to install the runtime files locally with "make install". However, in this case the best option would be to use Qt's resource system like ajshort recommends in his answer and embed the needed files in the executable.
I just started a Qt5 project using cmake as build system. So far this working quite well but I have problems with an icon that I want to load from a qrc file.
Project structure:
CMakeLists.txt
-- icons/
CMakeLists.txt
icons.qrc
locked.png
-- src/
CMakeLists.txt
source files...
Top Level CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11) #2.8.11 provides an easy way to build with qt5
project(fluchOmat)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library. This has dependencies on QtGui and QtCore!
find_package(Qt5Widgets REQUIRED)
# Find the QtMultimedia module
find_package(Qt5Multimedia REQUIRED)
add_subdirectory(icons)
add_subdirectory(src)
CMakeLists.txt in icons/
set(RESOURCE
${CMAKE_CURRENT_SOURCE_DIR}/icons.qrc
)
qt5_add_resources(RESOURCE_ADDED ${RESOURCE})
icons.qrc in icons/
<RCC>
<qresource prefix="/">
<file alias="locked">locked.png</file>
</qresource>
</RCC>
CMakeLists.txt in src/ (important part)
add_executable(fluchOmat ${SOURCES} ${HEADERS} ${fluchOmat_FORMS} ${RESOURCES_ADDED})
Some sample code
QPixmap pm(":/locked.png");
qDebug("Width: " + pm.width());
returns nothing, so obviously this didn't work.
There aren't many resources out there for Qt5 and cmake. Can anyone help me with this? Is this a cmake problem? For example I am not sure if qt5_add_resources is at the right location. Is my qrc file wrong?
Any help is greatly appreciated!
The problem lies in the scoping rules of CMake. each add_subdirectory call creates its own scope. Variables declared in such a subdirectory aren't automatically populated up to the parent scope.
So the problem is that while RESOURCE_ADDED contains a valid path in the scope of icons/CMakeLists.txt, it doesn't in the root CMakeLists.txt and hence it is an empty variable by the time you use it in src/CMakeLists.txt.
To raise the variable up a scope, in icons/CMakeLists.txt you can do:
qt5_add_resources(RESOURCE_ADDED ${RESOURCE})
set(RESOURCE_ADDED ${RESOURCE_ADDED} PARENT_SCOPE)
There's another slight problem now though!
While this will contain a valid value in src/CMakeLists.txt, it points to a file which doesn't yet exist. The qt5_add_resources function must apply the GENERATED source file property to the variable. This property is not carried forward to the variable set in the parent scope.
Since add_executable expects files to exist by default, you'll need to reapply the GENERATED property to the variable in the parent scope. You can do this e.g. in the src/CMakeLists.txt like this:
set_source_files_properties(${RESOURCE_ADDED} PROPERTIES GENERATED ON)
add_executable(fluchOmat ... ${RESOURCE_ADDED})
I'm not sure if the qt5_add_resources adds any other properties - if so, you'd maybe also have to reapply these.
I'd guess the easiest way to avoid this would be to not use add_subdirectory(icons), and instead just move all the Qt-related CMake code to src/CMakeLists.txt:
set(RESOURCE ${CMAKE_SOURCE_DIR}/icons/icons.qrc)
qt5_add_resources(RESOURCE_ADDED ${RESOURCE})
add_executable(fluchOmat ... ${RESOURCE_ADDED})