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.
備註
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.
備註
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::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
結果變數¶
該模組定義了以下變數:
Qt4_FOUNDBoolean indicating whether (the requested version of) Qt4 was found.
QT_FOUNDSame 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_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.libstatic 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
mocon 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 theQ_OBJECTdeclaration. 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_DIRECTORIESandINTERFACE_COMPILE_DEFINITIONStarget properties from the<target>are passed tomoc.OPTIONSOptional list of options given to
moc, such as those found when executingmoc -help.
備註
Instead of using
qt4_wrap_cpp(), theCMAKE_AUTOMOCvariable can be set to process source files withmocautomatically.
- qt4_wrap_ui¶
Creates build rules for running
uicon 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 executinguic -help.
備註
Instead of using
qt4_wrap_ui(), theCMAKE_AUTOUICvariable can be set to process ui files withuicautomatically.
- qt4_add_resources¶
Creates build rules for running
rccon 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 executingrcc -help.
備註
Instead of using
qt4_add_resources(), theCMAKE_AUTORCCvariable can be set to process resource files withrccautomatically.
- qt4_generate_moc¶
Creates a build rule that generates output file by running
mocon 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 reasonqt4_wrap_cpp()isn't feasible, e.g. because a custom filename is needed for the moc file or similar.TARGETIf specified, the
INTERFACE_INCLUDE_DIRECTORIESandINTERFACE_COMPILE_DEFINITIONStarget properties from the<target>are passed tomoc.
- 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
qdbusxml2cppcall 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.
- 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
.xmlappended. A relative path is interpreted as relative toCMAKE_CURRENT_BINARY_DIR.OPTIONSA list of options that may be given to
qdbuscpp2xml, such as those found when executingqdbuscpp2xml --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 vialupdateand QM (Qt Message files.qm) files vialreleasefrom 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.
- 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¶
在 2.8.11 版之後被棄用: Use feature provided by the
CMAKE_AUTOMOCvariable instead.Runs
mocon input files:qt4_automoc(<source-files>... [TARGET <target>])
This macro can be used to have
mocautomatically handled. For example, if there arefoo.handfoo.cppfiles, and infoo.ha class uses theQ_OBJECTpreprocessor macro,mochas to run on it. If usingqt4_wrap_cpp()isn't wanted (however, it is reliable and mature), the#include "foo.moc"can be inserted infoo.cppand thenfoo.cppgiven as argument toqt4_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 filefoo.h. If a source file has theSKIP_AUTOMOCproperty set, file will be ignored by this macro.TARGETIf specified, the
INTERFACE_INCLUDE_DIRECTORIESandINTERFACE_COMPILE_DEFINITIONStarget properties from the<target>are passed tomoc.
- qt4_use_modules¶
在 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_PUBLICorLINK_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 theQtCore,QtGuiandQtDeclarativecomponents on the project targetmyexe.
已棄用的變數¶
The following variables are provided for backward compatibility:
QT4_FOUND在 2.8.11 版之後被棄用: Use
Qt4_FOUND, which has the same value.
範例¶
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)