FindQt4 — CMake 4.4.2 Documentation

# FindQt4

Finds Qt4, a cross-platform application development framework for creating graphical user interfaces and applications:

    find_package(Qt4 [<version>] [...])

This module defines a number of imported targets, macros, and variables to use Qt4 in the project.

Note

This module is for Qt version 4. As of Qt version 5, the Qt upstream also provides an exported configuration to find Qt. New code should follow the `cmake-qt(7)` instead of using this module.

To detect the Qt4 package, the Qt4 `qmake` tool is required and must be available in the system path.

Note

When using Imported Targets, the `qtmain.lib` static library is automatically linked on Windows for `WIN32` executables. To disable this globally, set the `QT4_NO_LINK_QTMAIN` variable before finding Qt4. To disable this for a particular executable, set the `QT4_NO_LINK_QTMAIN` target property to `TRUE` on that executable.

## Qt Build Tools

Qt relies on some bundled tools for code generation, such as `moc` for meta-object code generation, `uic` for widget layout and population, and `rcc` for virtual filesystem content generation. These tools may be automatically invoked by `cmake(1)` if the appropriate conditions are met. See `cmake-qt(7)` for more.

## Imported Targets

Qt libraries can be linked using their corresponding Imported Target with the `target_link_libraries()` command:

    target_link_libraries(myexe Qt4::QtGui Qt4::QtXml)

Linking to an imported target automatically applies the correct include directories and compile definitions when building `myexe`.

Imported targets also manage their dependencies, so listing `Qt4::QtCore` is unnecessary if another Qt library depends on it. Likewise, `Qt4::QtGui` is automatically included when linking `Qt4::QtDeclarative`. Targets can be checked for existence using `if(TARGET)` command.

If both debug and release versions of a Qt toolkit library are available, CMake selects the appropriate one based on the build configuration.

This module provides the following imported targets, if found:

`Qt4::QtCore`

  The QtCore target

`Qt4::QtGui`

  The QtGui target

`Qt4::Qt3Support`

  The Qt3Support target

`Qt4::QtAssistant`

  The QtAssistant target

`Qt4::QtAssistantClient`

  The QtAssistantClient target

`Qt4::QAxContainer`

  The QAxContainer target (Windows only)

`Qt4::QAxServer`

  The QAxServer target (Windows only)

`Qt4::QtDBus`

  The QtDBus target

`Qt4::QtDeclarative`

  The QtDeclarative target

`Qt4::QtDesigner`

  The QtDesigner target

`Qt4::QtDesignerComponents`

  The QtDesignerComponents target

`Qt4::QtHelp`

  The QtHelp target

`Qt4::QtMotif`

  The QtMotif target

`Qt4::QtMultimedia`

  The QtMultimedia target

`Qt4::QtNetwork`

  The QtNetwork target

`Qt4::QtNsPlugin`

  The QtNsPlugin target

`Qt4::QtOpenGL`

  The QtOpenGL target

`Qt4::QtScript`

  The QtScript target

`Qt4::QtScriptTools`

  The QtScriptTools target

`Qt4::QtSql`

  The QtSql target

`Qt4::QtSvg`

  The QtSvg target

`Qt4::QtTest`

  The QtTest target

`Qt4::QtUiTools`

  The QtUiTools target

`Qt4::QtWebKit`

  The QtWebKit target

`Qt4::QtXml`

  The QtXml target

`Qt4::QtXmlPatterns`

  The QtXmlPatterns target

`Qt4::phonon`

  The phonon target

## Result Variables

This module defines the following variables:

`Qt4_FOUND`

  Boolean indicating whether (the requested version of) Qt4 was found.

`QT_FOUND`

  Same as `Qt4_FOUND`. Boolean indicating whether (the requested version of) Qt4 was found. This variable is available for compatibility with other Qt find modules.

`QT_VERSION_MAJOR`

  The major version of Qt found.

`QT_VERSION_MINOR`

  The minor version of Qt found.

`QT_VERSION_PATCH`

  The patch version of Qt found.

## Hints

`QT4_NO_LINK_QTMAIN`

  If set to boolean true before finding Qt4, it globally disables linking `qtmain.lib` static library on Windows.

## Macros

In some cases it can be necessary or useful to invoke the Qt build tools in a more-manual way. This module provides the following macros to add targets for such uses:

qt4_wrap_cpp

  Creates build rules for running `moc` on a given list of input files:

      qt4_wrap_cpp(<variable> <files>... [TARGET <target>] [OPTIONS <options>...])

  This macro creates build rules for processing a list of input files `<files>` that contain Qt classes with the `Q_OBJECT` declaration. Per-directory preprocessor definitions are also added.

  `<variable>`

    Name of a variable where a list of generated output files is stored.

  `<files>`

    One or more input source files.

  `TARGET`

    If specified, the `INTERFACE_INCLUDE_DIRECTORIES` and `INTERFACE_COMPILE_DEFINITIONS` target properties from the `<target>` are passed to `moc`.

  `OPTIONS`

    Optional list of options given to `moc`, such as those found when executing `moc -help`.

  Note

  Instead of using `qt4_wrap_cpp()`, the `CMAKE_AUTOMOC` variable can be set to process source files with `moc` automatically.

qt4_wrap_ui

  Creates build rules for running `uic` on a given list of Qt designer ui input files:

      qt4_wrap_ui(<variable> <files>... [OPTIONS <options>...])

  `<variable>`

    Name of a variable where a list of generated output filenames is stored.

  `<files>`

    One or more Qt designer ui input source files.

  `OPTIONS`

    Optional list of options given to `uic`, such as those found when executing `uic -help`.

  Note

  Instead of using `qt4_wrap_ui()`, the `CMAKE_AUTOUIC` variable can be set to process ui files with `uic` automatically.

qt4_add_resources

  Creates build rules for running `rcc` on a given list of input Qt resource files:

      qt4_add_resources(<variable> <files>... [OPTIONS <options>...])

  `<variable>`

    Name of a variable where a list of generated output filenames is stored.

  `<files>`

    One or more Qt resource input source files.

  `OPTIONS`

    Optional list of options given to `rcc`, such as those found when executing `rcc -help`.

  Note

  Instead of using `qt4_add_resources()`, the `CMAKE_AUTORCC` variable can be set to process resource files with `rcc` automatically.

qt4_generate_moc

  Creates a build rule that generates output file by running `moc` on a given input file.

      qt4_generate_moc(<input-file> <output-file> [TARGET <target>])

  This macro creates a build rule for `<input-file>` to generate `<output-file>`. Use this if for some reason `qt4_wrap_cpp()` isn't feasible, e.g. because a custom filename is needed for the moc file or similar.

  `TARGET`

    If specified, the `INTERFACE_INCLUDE_DIRECTORIES` and `INTERFACE_COMPILE_DEFINITIONS` target properties from the `<target>` are passed to `moc`.

qt4_add_dbus_interface

  Creates the interface header and implementation files from an interface XML file:

      qt4_add_dbus_interface(<variable> <interface-file> <basename>)

  This macro creates the interface header (`<basename>.h`) and implementation files (`<basename>.{cpp,moc}`) from the given interface XML file `<interface-file>` and adds it to the variable which contains a list of sources (specified as variable name `<variable>`).

  Additional parameters can be passed to the `qdbusxml2cpp` call by setting the following source file properties on the input file `<interface-file>`:

  `INCLUDE`

    The given file will be included in the generate interface header.

  `CLASSNAME`

    The name of the generated class.

  `NO_NAMESPACE`

    The generated class will not be wrapped in a namespace.

qt4_add_dbus_interfaces

  Creates the interface header and implementation files from multiple interface XML files:

      qt4_add_dbus_interfaces(<variable> <interface-files>...)

  This macro creates the interface header and implementation files for all listed interface XML files `<interface-files>`. The basename will be automatically determined from the name of the XML file. The resulting output files list is stored in a variable `<variable>`.

  The source file properties described for `qt4_add_dbus_interface()` also apply here.

qt4_add_dbus_adaptor

  Generates an adaptor class for a D-Bus interface:

      qt4_add_dbus_adaptor(<variable> <xmlfile> <parent-header> <parent-classname>
                           [<basename>] [<classname>])

  Creates a D-Bus adaptor (header and implementation file) from the XML file describing the interface, and adds it to the list of sources. The adaptor forwards the calls to a parent class, defined in `<parent-header>` and named `<parent-classname>`. The generated filenames will be `<basename>adaptor.{cpp,h}` where `<basename>` defaults to the basename of the XML file if not given. If `<classname>` is provided, then it will be used as the classname of the adaptor itself. Generated filenames are stored in a variable `<variable>`.

qt4_generate_dbus_interface

  Generates a D-Bus XML interface file from a given header file:

      qt4_generate_dbus_interface(<header> [<interface>] [OPTIONS <options>...])

  This macro creates a build rule to extract declaration from the given `<header>` file to generate a corresponding XML interface file.

  `<header>`

    Path to header file from which XML interface file is generated.

  `<interface>`

    Path to the generated XML interface file. If this optional argument is omitted, the name of the interface file is constructed from the basename of the header with the suffix `.xml` appended. A relative path is interpreted as relative to `CMAKE_CURRENT_BINARY_DIR`.

  `OPTIONS`

    A list of options that may be given to `qdbuscpp2xml`, such as those found when executing `qdbuscpp2xml --help`.

qt4_create_translation

  Creates build rules for generating TS and QM files:

      qt4_create_translation(<qm-files-var> <directories>... <sources>...
                             <ts-files>... [OPTIONS <options>...])

  This macro creates build rules to generate TS (Translation Source files `.ts`) files via `lupdate` and QM (Qt Message files `.qm`) files via `lrelease` from the given `<directories>` and/or `<sources>`. The TS files are created and/or updated in the source tree (unless given with full paths). The QM files are generated in the build tree.

  `<qm-files-var>`

    A list of generated QM files is stored in this variable. Updating the translations can be done by adding the `<qm-files-var>` to the source list of the project library/executable, so they are always updated, or by adding a custom target to control when they get updated/generated.

  `<directories>`

    A list of directories containing source files.

  `<sources>`

    A list of source files.

  `<ts-files>`

    A list of TS (Translation Source) files.

  `OPTIONS`

    Optional list of flags passed to `lupdate`, such as `-extensions`, to specify file extensions for directory scanning.

qt4_add_translation

  Creates build rules for generating QM files from the given TS files:

      qt4_add_translation(<qm-files-var> <ts-files>...)

  This macro creates build rules for generating QM files from the given TS files and stores a list of generated filenames of QM files in the `<qm-files-var>` variable. The `<ts-files>` must exist and are not updated in any way.

qt4_automoc

  Deprecated since version 2.8.11: Use feature provided by the `CMAKE_AUTOMOC` variable instead.

  Runs `moc` on input files:

      qt4_automoc(<source-files>... [TARGET <target>])

  This macro can be used to have `moc` automatically handled. For example, if there are `foo.h` and `foo.cpp` files, and in `foo.h` a class uses the `Q_OBJECT` preprocessor macro, `moc` has to run on it. If using `qt4_wrap_cpp()` isn't wanted (however, it is reliable and mature), the `#include "foo.moc"` can be inserted in `foo.cpp` and then `foo.cpp` given as argument to `qt4_automoc()`. This will scan all listed files `<source-files>` at configuration phase for such included moc files and if it finds them, a rule is generated to run moc at build time on the accompanying header file `foo.h`. If a source file has the `SKIP_AUTOMOC` property set, file will be ignored by this macro.

  `TARGET`

    If specified, the `INTERFACE_INCLUDE_DIRECTORIES` and `INTERFACE_COMPILE_DEFINITIONS` target properties from the `<target>` are passed to `moc`.

qt4_use_modules

  Deprecated since version 2.8.11: Use `target_link_libraries()` with Imported Targets instead.

  Provides Qt modules to a project for linking them to a target:

      qt4_use_modules(<target> [<LINK_PUBLIC|LINK_PRIVATE>] <modules>...)

  This function makes `<target>` use the `<modules>` from Qt. Using a Qt module means to link to the library, add the relevant include directories for the module, and add the relevant compiler defines for using the module. Modules are roughly equivalent to Qt4 components.

  `LINK_PUBLIC` or `LINK_PRIVATE`

    Optional linking mode, used as the corresponding argument in the `target_link_libraries()` call.

  For example, calling `qt4_use_modules(myexe Core Gui Declarative)` will use the `QtCore`, `QtGui` and `QtDeclarative` components on the project target `myexe`.

## Deprecated Variables

The following variables are provided for backward compatibility:

`QT4_FOUND`

  Deprecated since version 2.8.11: Use `Qt4_FOUND`, which has the same value.

## Examples

Typical usage to find Qt4, could be something like:

    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    find_package(Qt4 4.4.3 REQUIRED QtGui QtXml)
    add_executable(myexe main.cpp)
    target_link_libraries(myexe PRIVATE Qt4::QtGui Qt4::QtXml)