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 目標時,對於 WIN32 可執行檔,qtmain.lib 靜態函式庫會在 Windows 上自動連結。若要全域停用此行為,請在尋找 Qt4 之前設定 QT4_NO_LINK_QTMAIN 變數。若要針對特定的可執行檔停用此行為,請將可執行檔的 QT4_NO_LINK_QTMAIN 目標屬性設定為 TRUE

Qt 建置工具

Qt 依賴一些捆綁的工具來產生程式碼,例如用於 meta-object 程式碼產生的 moc、用於widget 佈局和填充的 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.

匯入目標

特定的 Qt 函式庫可以透過使用對應的 IMPORTED 目標與 target_link_libraries() 命令來使用

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

以這種方式使用目標會導致 `:cmake(1)` 在編譯 myexe 時使用目標的適當 include 目錄和編譯定義。

目標知道它們的依賴關係,因此舉例來說,如果列出了另一個 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

如果為 false,請勿嘗試使用 Qt 4。

QT_FOUND

如果為 false,請勿嘗試使用 Qt。此變數僅為相容性而存在。

QT4_FOUND

如果為 false,請勿嘗試使用 Qt 4。此變數僅為相容性而存在。

QT_VERSION_MAJOR

找到的 Qt 主要版本。

QT_VERSION_MINOR

找到的 Qt 次要版本。

QT_VERSION_PATCH

找到的 Qt 修補版本。