Ilya Kirillov
Ilya Kirillov

Reputation: 29

Copy all qt resources using cmake

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?

Upvotes: 2

Views: 2996

Answers (3)

Dawars
Dawars

Reputation: 57

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

Upvotes: 1

juzzlin
juzzlin

Reputation: 47875

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.

Upvotes: 0

ajshort
ajshort

Reputation: 3754

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).

Upvotes: 4

Related Questions