0

I have a Visual Studio 2017 project that is built with QT and VTK using CMake. I need to use image resources, and wish to use QT's .qrc resource system.

Information on this seems rare and complex when not using a QT project file. I have found this:

  1. Create a .qrc file that contains a list of the resources you would like to include

  2. Create a custom build step that invokes rcc on that file (documentation)

  3. Compile and link the resulting cpp source file into your program.

How to use Qt resource files in Visual Studio without a Qt project?

I am just completely confused right now on what the proper way to go about this is, and this is not enough information.

drescherjm
  • 10,365
  • 5
  • 44
  • 64

1 Answers1

4

Add this to your CMake:

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

This will run moc and rcc compiler automatically.

Then use the following to add the resources and link them

# Compiles binary resources into source code and puts the names into RESOURCES variable
qt5_add_resources(RESOURCES example.qrc)

# Adds the RESOURCES source code to your application so it will be linked
# and part of your executable
add_executable(exampleApplication main.cpp ${RESOURCES})

Further reading:

deW1
  • 5,562
  • 10
  • 38
  • 54
  • thank you so much, I just don't understand the second block... specifically the add executable, I already have something like that in my CMake file. – AlexanderAlexander Sep 23 '19 at 12:10
  • @AlexanderAlexander I have added some more explanation. I hope that makes it clearer. Otherwise you will have to explain what you don't understand specifically. – deW1 Sep 23 '19 at 12:17
  • Thank you very much for the comments I am just confused where to place the second block. Can I just put it at end of CMake file, after the autoMOC and RCC? – AlexanderAlexander Sep 23 '19 at 12:23
  • I also already call add_executable in my file so adding this one causes an error when building – AlexanderAlexander Sep 23 '19 at 12:27
  • You put `qt5_add_resources` before your `add_executable` call and add `${RESOURCES}` to your already existing `add_executable` just like you would any additional file. – deW1 Sep 23 '19 at 12:29