cmake-qt.7.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. .. cmake-manual-description: CMake Qt Features Reference
  2. cmake-qt(7)
  3. ***********
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. CMake can find and use Qt 4 and Qt 5 libraries. The Qt 4 libraries are found
  9. by the :module:`FindQt4` find-module shipped with CMake, whereas the
  10. Qt 5 libraries are found using "Config-file Packages" shipped with Qt 5. See
  11. :manual:`cmake-packages(7)` for more information about CMake packages, and
  12. see `the Qt cmake manual <http://qt-project.org/doc/qt-5/cmake-manual.html>`_
  13. for your Qt version.
  14. Qt 4 and Qt 5 may be used together in the same
  15. :manual:`CMake buildsystem <cmake-buildsystem(7)>`:
  16. .. code-block:: cmake
  17. cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
  18. project(Qt4And5)
  19. set(CMAKE_AUTOMOC ON)
  20. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  21. find_package(Qt5Widgets REQUIRED)
  22. add_executable(publisher publisher.cpp)
  23. target_link_libraries(publisher Qt5::Widgets Qt5::DBus)
  24. find_package(Qt4 REQUIRED)
  25. add_executable(subscriber subscriber.cpp)
  26. target_link_libraries(subscriber Qt4::QtGui Qt4::QtDBus)
  27. A CMake target may not link to both Qt 4 and Qt 5. A diagnostic is issued if
  28. this is attempted or results from transitive target dependency evaluation.
  29. Qt Build Tools
  30. ==============
  31. Qt relies on some bundled tools for code generation, such as ``moc`` for
  32. meta-object code generation, ``uic`` for widget layout and population,
  33. and ``rcc`` for virtual filesystem content generation. These tools may be
  34. automatically invoked by :manual:`cmake(1)` if the appropriate conditions
  35. are met. The automatic tool invocation may be used with both Qt 4 and Qt 5.
  36. The tools are executed as part of a synthesized custom target generated by
  37. CMake. Target dependencies may be added to that custom target by adding them
  38. to the :prop_tgt:`AUTOGEN_TARGET_DEPENDS` target property.
  39. AUTOMOC
  40. ^^^^^^^
  41. The :prop_tgt:`AUTOMOC` target property controls whether :manual:`cmake(1)`
  42. inspects the C++ files in the target to determine if they require ``moc`` to
  43. be run, and to create rules to execute ``moc`` at the appropriate time.
  44. If a ``Q_OBJECT`` or ``Q_GADGET`` macro is found in a header file, ``moc``
  45. will be run on the file. The result will be put into a file named according
  46. to ``moc_<basename>.cpp``. If the macro is found in a C++ implementation
  47. file, the moc output will be put into a file named according to
  48. ``<basename>.moc``, following the Qt conventions. The ``moc file`` may be
  49. included by the user in the C++ implementation file with a preprocessor
  50. ``#include``. If it is not so included, it will be added to a separate file
  51. which is compiled into the target.
  52. The ``moc`` command line will consume the :prop_tgt:`COMPILE_DEFINITIONS` and
  53. :prop_tgt:`INCLUDE_DIRECTORIES` target properties from the target it is being
  54. invoked for, and for the appropriate build configuration.
  55. Generated ``moc_*.cpp`` and ``*.moc`` files are placed in the build directory
  56. so it is convenient to set the :variable:`CMAKE_INCLUDE_CURRENT_DIR`
  57. variable. The :prop_tgt:`AUTOMOC` target property may be pre-set for all
  58. following targets by setting the :variable:`CMAKE_AUTOMOC` variable. The
  59. :prop_tgt:`AUTOMOC_MOC_OPTIONS` target property may be populated to set
  60. options to pass to ``moc``. The :variable:`CMAKE_AUTOMOC_MOC_OPTIONS`
  61. variable may be populated to pre-set the options for all following targets.
  62. .. _`Qt AUTOUIC`:
  63. AUTOUIC
  64. ^^^^^^^
  65. The :prop_tgt:`AUTOUIC` target property controls whether :manual:`cmake(1)`
  66. inspects the C++ files in the target to determine if they require ``uic`` to
  67. be run, and to create rules to execute ``uic`` at the appropriate time.
  68. If a preprocessor ``#include`` directive is found which matches
  69. ``ui_<basename>.h``, and a ``<basename>.ui`` file exists, then ``uic`` will
  70. be executed to generate the appropriate file.
  71. Generated ``ui_*.h`` files are placed in the build directory so it is
  72. convenient to set the :variable:`CMAKE_INCLUDE_CURRENT_DIR` variable. The
  73. :prop_tgt:`AUTOUIC` target property may be pre-set for all following targets
  74. by setting the :variable:`CMAKE_AUTOUIC` variable. The
  75. :prop_tgt:`AUTOUIC_OPTIONS` target property may be populated to set options
  76. to pass to ``uic``. The :variable:`CMAKE_AUTOUIC_OPTIONS` variable may be
  77. populated to pre-set the options for all following targets. The
  78. :prop_sf:`AUTOUIC_OPTIONS` source file property may be set on the
  79. ``<basename>.ui`` file to set particular options for the file. This
  80. overrides options from the :prop_tgt:`AUTOUIC_OPTIONS` target property.
  81. A target may populate the :prop_tgt:`INTERFACE_AUTOUIC_OPTIONS` target
  82. property with options that should be used when invoking ``uic``. This must be
  83. consistent with the :prop_tgt:`AUTOUIC_OPTIONS` target property content of the
  84. depender target. The :variable:`CMAKE_DEBUG_TARGET_PROPERTIES` variable may
  85. be used to track the origin target of such
  86. :prop_tgt:`INTERFACE_AUTOUIC_OPTIONS`. This means that a library which
  87. provides an alternative translation system for Qt may specify options which
  88. should be used when running ``uic``:
  89. .. code-block:: cmake
  90. add_library(KI18n klocalizedstring.cpp)
  91. target_link_libraries(KI18n Qt5::Core)
  92. # KI18n uses the tr2i18n() function instead of tr(). That function is
  93. # declared in the klocalizedstring.h header.
  94. set(autouic_options
  95. -tr tr2i18n
  96. -include klocalizedstring.h
  97. )
  98. set_property(TARGET KI18n APPEND PROPERTY
  99. INTERFACE_AUTOUIC_OPTIONS ${autouic_options}
  100. )
  101. A consuming project linking to the target exported from upstream automatically
  102. uses appropriate options when ``uic`` is run by :prop_tgt:`AUTOUIC`, as a
  103. result of linking with the :prop_tgt:`IMPORTED` target:
  104. .. code-block:: cmake
  105. set(CMAKE_AUTOUIC ON)
  106. # Uses a libwidget.ui file:
  107. add_library(LibWidget libwidget.cpp)
  108. target_link_libraries(LibWidget
  109. KF5::KI18n
  110. Qt5::Widgets
  111. )
  112. .. _`Qt AUTORCC`:
  113. AUTORCC
  114. ^^^^^^^
  115. The :prop_tgt:`AUTORCC` target property controls whether :manual:`cmake(1)`
  116. creates rules to execute ``rcc`` at the appropriate time on source files
  117. which have the suffix ``.qrc``.
  118. .. code-block:: cmake
  119. add_executable(myexe main.cpp resource_file.qrc)
  120. The :prop_tgt:`AUTORCC` target property may be pre-set for all following targets
  121. by setting the :variable:`CMAKE_AUTORCC` variable. The
  122. :prop_tgt:`AUTORCC_OPTIONS` target property may be populated to set options
  123. to pass to ``rcc``. The :variable:`CMAKE_AUTORCC_OPTIONS` variable may be
  124. populated to pre-set the options for all following targets. The
  125. :prop_sf:`AUTORCC_OPTIONS` source file property may be set on the
  126. ``<name>.qrc`` file to set particular options for the file. This
  127. overrides options from the :prop_tgt:`AUTORCC_OPTIONS` target property.
  128. qtmain.lib on Windows
  129. =====================
  130. The Qt 4 and 5 :prop_tgt:`IMPORTED` targets for the QtGui libraries specify
  131. that the qtmain.lib static library shipped with Qt will be linked by all
  132. dependent executables which have the :prop_tgt:`WIN32_EXECUTABLE` enabled.
  133. To disable this behavior, enable the ``Qt5_NO_LINK_QTMAIN`` target property for
  134. Qt 5 based targets or ``QT4_NO_LINK_QTMAIN`` target property for Qt 4 based
  135. targets.
  136. .. code-block:: cmake
  137. add_executable(myexe WIN32 main.cpp)
  138. target_link_libraries(myexe Qt4::QtGui)
  139. add_executable(myexe_no_qtmain WIN32 main_no_qtmain.cpp)
  140. set_property(TARGET main_no_qtmain PROPERTY QT4_NO_LINK_QTMAIN ON)
  141. target_link_libraries(main_no_qtmain Qt4::QtGui)