This cheatsheet contains solutions for typical tasks that a LeechCraft developer may face.

We will mostly use code snippets from the CMakeLists.txt of the Auscrie plugin, which is listed fully at the end of the page.

Adding new source files



Add the names of the source (*.cpp) files you want to the SRCS variable if you’re using the CMakeLists.txt generated by default by our genplugin.py script. So, for example, if you had

set (SRCS
    auscrie.cpp
    shooterdialog.cpp
    poster.cpp
    )
and you've written a class, say, RequestBuilder with requestbuilder.h and requestbuilder.cpp header/implementation files, you should have:

set (SRCS
    auscrie.cpp
    shooterdialog.cpp
    poster.cpp
    requestbuilder.cpp
    )

Note: Starting with CMake 2.8.6 we rely on the automoc feature to automatically run Qt's moc over headers with Q_OBJECT macro. That's why there is no HEADERS variable anymore. For this feature to work, though, every such header should be included at least once in source files listed in the SRCS variable.

Handling .ui files

Set a variable with the list of .ui files like the ones with headers and sources and pass it to the QT4_WRAP_UI command. For example, for two .ui files shooterdialog.ui and savedialog.ui, the following code would be written:

set (FORMS
    shooterdialog.ui
    savedialog.ui
    )
QT4_WRAP_UI (UIS_H ${FORMS})

After that, the UIS_H would contain the list of headers generated by the uic. It should be included as the dependency for your library:

add_library (leechcraft_auscrie SHARED
    ...
    ${UIS_H}
    ...
    )

Adding resources

To add the resources file, say, anheroresources.qrc, add the following to the CMakeLists.txt, before the ADD_LIBRARY call:

set (RESOURCES auscrieresources.qrc)
QT4_ADD_RESOURCES (RCCS ${RESOURCES})
Don't forget to add the ${RCCS} to the list of dependencies for your library. For the above example with AnHero:

add_library (leechcraft_auscrie SHARED
    ...
    ${RCCS}
    ...
    )

Adding translations

Use the CreateTrs macro defined in LeechCraft cmake files, like:

CreateTrs ("auscrie" "de;en;es;fr;it;oc;ru_RU;uk_UA" COMPILED_TRANSLATIONS)

Here you pass the name of your plugin as the first parameter, the ;-separated list of translations languages as the second one and the variable name for the list of files with compiled translations (*.qm) as third parameter.

CreateTrs will take care running the lrelease executable over your translation files, so just don't forget to add the compiled translations as the dependency of your plugin:

add_library (leechcraft_auscrie SHARED
    ...
    ${COMPILED_TRANSLATIONS}
    ...
    )

Don't also forget to install the resulting files if you don't embed them into resources (and this is the recommended scheme). See the next section regarding installing.

Installing arbitrary files

Just use the INSTALL macro.

To install a list of files, for example, the compiled translations from the previous section:

install (FILES ${COMPILED_TRANSLATIONS} DESTINATION ${LC_TRANSLATIONS_DEST})



Of course, you can hardcode the installation path instead of using variables. For that, just replace the ${LC_TRANSLATIONS_DEST} with the corresponding string.

Installing (and using) QML files



A recommended approach of using QML files is to install them into ${LC_QML_DEST} and load with the Util::GetSysPath method.

So, place your QML files into the share/qml/pluginname subdirectory of your plugin directory. Install the whole subdirectory:

install (DIRECTORY share/qml/pluginname DESTINATION ${LC_QML_DEST})

and get the path to a QML file in your C++ code like this:

const auto& filepath = Util::GetSysPath (Util::SysPath::QML, "pluginname", "Filename.qml");



Don’t forget to check the filepath if it’s empty just in case!

Adding Qt modules besides Core and GUI



To include support for other Qt modules but Core and GUI, set the corresponding QT_USE_* variable before the line with INCLUDE (${QT_USE_FILE}). The full list of the variables is given at the CMake doc page.

For example, for a plugin that needs QtNetwork as well:

set (QT_USE_QTNETWORK TRUE)
include (${QT_USE_FILE})

CMakeLists.txt from the Auscrie plugin



CMAKE_MINIMUM_REQUIRED (VERSION 2.8)
PROJECT (leechcraft_auscrie)
INCLUDE (InitLCPlugin OPTIONAL)

SET (QT_USE_QTNETWORK TRUE)
INCLUDE (${QT_USE_FILE})
INCLUDE_DIRECTORIES (
	${CMAKE_CURRENT_BINARY_DIR}
	${Boost_INCLUDE_DIR}
	${LEECHCRAFT_INCLUDE_DIR}
	)
SET (SRCS
	auscrie.cpp
	shooterdialog.cpp
	poster.cpp
	requestbuilder.cpp
	)
SET (FORMS
	shooterdialog.ui
	)
SET (RESOURCES auscrieresources.qrc)
CreateTrs ("auscrie" "de;en;es;fr;it;oc;ru_RU;uk_UA" COMPILED_TRANSLATIONS)
QT4_WRAP_UI (UIS_H ${FORMS})
QT4_ADD_RESOURCES (RCCS ${RESOURCES})

ADD_LIBRARY (leechcraft_auscrie SHARED
	${COMPILED_TRANSLATIONS}
	${SRCS}
	${MOC_SRCS}
	${UIS_H}
	${RCCS}
	)
TARGET_LINK_LIBRARIES (leechcraft_auscrie
	${QT_LIBRARIES}
	${LEECHCRAFT_LIBRARIES}
	)
INSTALL (TARGETS leechcraft_auscrie DESTINATION ${LC_PLUGINS_DEST})
INSTALL (FILES ${COMPILED_TRANSLATIONS} DESTINATION ${LC_TRANSLATIONS_DEST})