FindQt4

查找和使用 Qt4

该模块可用于查找 Qt4。最重要的问题是 Qt4 qmake 可通过系统路径使用。然后使用该 qmake 基本检测所有其他内容。该模块定义了许多 IMPORTED 目标、宏和变量。

典型用法可能是这样的

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 Qt4::QtGui Qt4::QtXml)

注释

使用 IMPORTED 目标时,将在 Windows 上自动链接 WIN32 可执行文件的 qtmain.lib 静态库。要全局禁用此功能,请在查找 Qt4 之前设置 QT4_NO_LINK_QTMAIN 变量。要为特定可执行文件禁用此功能,请将可执行文件的 QT4_NO_LINK_QTMAIN 目标属性设置为 TRUE

Qt 构建工具

Qt 依赖于一些捆绑工具进行代码生成,例如 moc 用于生成元对象代码,uic 用于生成窗口小部件布局和填充,rcc 用于生成虚拟文件系统内容。如果满足适当的条件,cmake(1) 可以自动调用这些工具。详情请参阅 cmake-qt(7)

Qt 宏

在某些情况下,需要或可用于手动调用 Qt 构建工具。有若干可用的宏用于添加此类用途的目标。

macro QT4_WRAP_CPP(outfiles inputfile ... [TARGET tgt] OPTIONS ...)
      create moc code from a list of files containing Qt class with
      the Q_OBJECT declaration.  Per-directory preprocessor definitions
      are also added.  If the <tgt> is specified, the
      INTERFACE_INCLUDE_DIRECTORIES and INTERFACE_COMPILE_DEFINITIONS from
      the <tgt> are passed to moc.  Options may be given to moc, such as
      those found when executing "moc -help".
macro QT4_WRAP_UI(outfiles inputfile ... OPTIONS ...)
      create code from a list of Qt designer ui files.
      Options may be given to uic, such as those found
      when executing "uic -help"
macro QT4_ADD_RESOURCES(outfiles inputfile ... OPTIONS ...)
      create code from a list of Qt resource files.
      Options may be given to rcc, such as those found
      when executing "rcc -help"
macro QT4_GENERATE_MOC(inputfile outputfile [TARGET tgt])
      creates a rule to run moc on infile and create outfile.
      Use this if for some reason QT4_WRAP_CPP() isn't appropriate, e.g.
      because you need a custom filename for the moc file or something
      similar.  If the <tgt> is specified, the
      INTERFACE_INCLUDE_DIRECTORIES and INTERFACE_COMPILE_DEFINITIONS from
      the <tgt> are passed to moc.
macro QT4_ADD_DBUS_INTERFACE(outfiles interface basename)
      Create the interface header and implementation files with the
      given basename from the given interface xml file and add it to
      the list of sources.

      You can pass additional parameters to the qdbusxml2cpp call by setting
      properties on the input file:

      INCLUDE the given file will be included in the generate interface header

      CLASSNAME the generated class is named accordingly

      NO_NAMESPACE the generated class is not wrapped in a namespace
macro QT4_ADD_DBUS_INTERFACES(outfiles inputfile ... )
      Create the interface header and implementation files
      for all listed interface xml files.
      The basename will be automatically determined from the name
      of the xml file.

      The source file properties described for
      QT4_ADD_DBUS_INTERFACE also apply here.
macro QT4_ADD_DBUS_ADAPTOR(outfiles xmlfile parentheader parentclassname
                           [basename] [classname])
      create a dbus adaptor (header and implementation file) from the xml file
      describing the interface, and add it to the list of sources. The adaptor
      forwards the calls to a parent class, defined in parentheader and named
      parentclassname. The name of the generated files will be
      <basename>adaptor.{cpp,h} where basename defaults to the basename of the
      xml file.
      If <classname> is provided, then it will be used as the classname of the
      adaptor itself.
macro QT4_GENERATE_DBUS_INTERFACE( header [interfacename] OPTIONS ...)
      generate the xml interface file from the given header.
      If the optional argument interfacename is omitted, the name of the
      interface file is constructed from the basename of the header with
      the suffix .xml appended.
      Options may be given to qdbuscpp2xml, such as those found when
      executing "qdbuscpp2xml --help"
macro QT4_CREATE_TRANSLATION( qm_files directories ... sources ...
                              ts_files ... OPTIONS ...)
      out: qm_files
      in:  directories sources ts_files
      options: flags to pass to lupdate, such as -extensions to specify
      extensions for a directory scan.
      generates commands to create .ts (via lupdate) and .qm
      (via lrelease) - files from 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.
      Updating the translations can be done by adding the qm_files
      to the source list of your library/executable, so they are
      always updated, or by adding a custom target to control when
      they get updated/generated.
macro QT4_ADD_TRANSLATION( qm_files ts_files ... )
      out: qm_files
      in:  ts_files
      generates commands to create .qm from .ts - files. The generated
      filenames can be found in qm_files. The ts_files
      must exist and are not updated in any way.
macro QT4_AUTOMOC(sourcefile1 sourcefile2 ... [TARGET tgt])
      The qt4_automoc macro is obsolete.  Use the CMAKE_AUTOMOC feature instead.
      This macro is still experimental.
      It can be used to have moc automatically handled.
      So if you have the files foo.h and foo.cpp, and in foo.h a
      a class uses the Q_OBJECT macro, moc has to run on it. If you don't
      want to use QT4_WRAP_CPP() (which is reliable and mature), you can insert
      #include "foo.moc"
      in foo.cpp and then give foo.cpp as argument to QT4_AUTOMOC(). This will
      scan all listed files at cmake-time for such included moc files and if it
      finds them cause a rule to be generated to run moc at build time on the
      accompanying header file foo.h.
      If a source file has the SKIP_AUTOMOC property set it will be ignored by
      this macro.
      If the <tgt> is specified, the INTERFACE_INCLUDE_DIRECTORIES and
      INTERFACE_COMPILE_DEFINITIONS from the <tgt> are passed to moc.
function QT4_USE_MODULES( target [link_type] modules...)
       This function is obsolete. Use target_link_libraries with IMPORTED targets
       instead.
       Make <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 components of Qt4, so usage would be
       something like:
        qt4_use_modules(myexe Core Gui Declarative)
       to use QtCore, QtGui and QtDeclarative. The optional <link_type> argument
       can be specified as either LINK_PUBLIC or LINK_PRIVATE to specify the
       same argument to the target_link_libraries call.

IMPORTED 目标

可以通过 IMPORTED 目标使用特定 Qt 库,并通过 target_link_libraries() 命令使用相应的库

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

以这种方式使用目标导致在编译 myexe 时,:cmake(1)` 使用目标的相应头包含目录和编译定义。

目标会知道其依赖项,因此,比如当列出另一个 Qt 函数库时,无需列出 Qt4::QtCore,且当列出 Qt4::QtDeclarative 时,无需列出 Qt4::QtGui。可使用 if(TARGET) 以通常的方式测试目标是否存在。

Qt 工具包中可能包含调试和发布函数库。cmake(1) 将基于构建配置选择合适的版本。

Qt4::QtCore

QtCore 目标

Qt4::QtGui

QtGui 目标

Qt4::Qt3Support

Qt3Support 目标

Qt4::QtAssistant

QtAssistant 目标

Qt4::QtAssistantClient

QtAssistantClient 目标

Qt4::QAxContainer

QAxContainer 目标(仅限 Windows)

Qt4::QAxServer

QAxServer 目标(仅限 Windows)

Qt4::QtDBus

QtDBus 目标

Qt4::QtDeclarative

QtDeclarative 目标

Qt4::QtDesigner

QtDesigner 目标

Qt4::QtDesignerComponents

QtDesignerComponents 目标

Qt4::QtHelp

QtHelp 目标

Qt4::QtMotif

QtMotif 目标

Qt4::QtMultimedia

QtMultimedia 目标

Qt4::QtNetwork

QtNetwork 目标

Qt4::QtNsPLugin

QtNsPLugin 目标

Qt4::QtOpenGL

QtOpenGL 目标

Qt4::QtScript

QtScript 目标

Qt4::QtScriptTools

QtScriptTools 目标

Qt4::QtSql

QtSql 目标

Qt4::QtSvg

QtSvg 目标

Qt4::QtTest

QtTest 目标

Qt4::QtUiTools

QtUiTools 目标

Qt4::QtWebKit

QtWebKit 目标

Qt4::QtXml

QtXml 目标

Qt4::QtXmlPatterns

QtXmlPatterns 目标

Qt4::phonon

phonon 目标

目标变量

以下是 FindQt4.cmake 设置的变量的详细列表。

Qt4_FOUND

如果假,请不要尝试使用 Qt 4。

QT_FOUND

如果假,请不要尝试使用 Qt。此变量仅用于兼容性。

QT4_FOUND

如果假,请不要尝试使用 Qt 4。此变量仅用于兼容性。

QT_VERSION_MAJOR

找到的 Qt 的主要版本。

QT_VERSION_MINOR

找到的 Qt 的次要版本。

QT_VERSION_PATCH

找到的 Qt 的补丁版本。