This module finds Qt4, a cross-platform application development framework for creating graphical user interfaces and applications. It 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 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.
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::QtCoreThe QtCore target
Qt4::QtGuiThe QtGui target
Qt4::Qt3SupportThe Qt3Support target
Qt4::QtAssistantThe QtAssistant target
Qt4::QtAssistantClientThe QtAssistantClient target
Qt4::QAxContainerThe QAxContainer target (Windows only)
Qt4::QAxServerThe QAxServer target (Windows only)
Qt4::QtDBusThe QtDBus target
Qt4::QtDeclarativeThe QtDeclarative target
Qt4::QtDesignerThe QtDesigner target
Qt4::QtDesignerComponentsThe QtDesignerComponents target
Qt4::QtHelpThe QtHelp target
Qt4::QtMotifThe QtMotif target
Qt4::QtMultimediaThe QtMultimedia target
Qt4::QtNetworkThe QtNetwork target
Qt4::QtNsPluginThe QtNsPlugin target
Qt4::QtOpenGLThe QtOpenGL target
Qt4::QtScriptThe QtScript target
Qt4::QtScriptToolsThe QtScriptTools target
Qt4::QtSqlThe QtSql target
Qt4::QtSvgThe QtSvg target
Qt4::QtTestThe QtTest target
Qt4::QtUiToolsThe QtUiTools target
Qt4::QtWebKitThe QtWebKit target
Qt4::QtXmlThe QtXml target
Qt4::QtXmlPatternsThe QtXmlPatterns target
Qt4::phononThe phonon target
This module sets the following variables:
Qt4_FOUNDBoolean whether Qt4 has been found. If false, don't try to use Qt4.
QT_FOUNDBoolean whether Qt4 has been found. If false, don't try to use Qt4. This variable is for compatibility with other Qt find modules.
QT4_FOUNDBoolean whether Qt4 has been found. If false, don't try to use Qt4. This variable is for backward compatibility only.
QT_VERSION_MAJORThe major version of Qt found.
QT_VERSION_MINORThe minor version of Qt found.
QT_VERSION_PATCHThe patch version of Qt found.
QT4_NO_LINK_QTMAINIf set to boolean true before finding Qt4, it globally disables linking
qtmain.lib static library on Windows.
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:
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.
TARGETIf specified, the INTERFACE_INCLUDE_DIRECTORIES and
INTERFACE_COMPILE_DEFINITIONS target properties from the <target>
are passed to moc.
OPTIONSOptional 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.
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.
OPTIONSOptional 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.
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.
OPTIONSOptional 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.
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.
TARGETIf specified, the INTERFACE_INCLUDE_DIRECTORIES and
INTERFACE_COMPILE_DEFINITIONS target properties from the <target>
are passed to moc.
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>:
INCLUDEThe given file will be included in the generate interface header.
CLASSNAMEThe name of the generated class.
NO_NAMESPACEThe generated class will not be wrapped in a namespace.
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.
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>.
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.
OPTIONSA list of options that may be given to qdbuscpp2xml, such as those found
when executing qdbuscpp2xml --help.
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.
OPTIONSOptional list of flags passed to lupdate, such as -extensions, to
specify file extensions for directory scanning.
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.
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.
TARGETIf specified, the INTERFACE_INCLUDE_DIRECTORIES and
INTERFACE_COMPILE_DEFINITIONS target properties from the <target>
are passed to moc.
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_PRIVATEOptional 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.
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)