6.12. 使用 Qt

6.12.1. 需要 Qt 的 Ports

The Ports Collection provides support for Qt 4 and Qt 5 frameworks with USE_QTx, where x is 4 or 5. Set USE_QTx to the list of required Qt components (libraries, tools, plugins). The Qt 4 and Qt 5 frameworks are quite similar. The main difference is the set of supported components.

The Qt framework exports a number of variables which can be used by ports, some of them listed below:

表格 6.11. Variables Provided to Ports That Use Qt
QT_PREFIXSet to the path where Qt was installed (${LOCALBASE}).
QMAKEFull path to qmake binary.
LRELEASEFull path to lrelease utility.
MOCFull path to moc.
RCCFull path to rcc.
UICFull path to uic.
QT_INCDIRQt include directory.
QT_LIBDIRQt libraries path.
QT_PLUGINDIRQt plugins path.

When using the Qt framework, these settings are deployed:

CONFIGURE_ARGS+=	--with-qt-includes=${QT_INCDIR} \
			--with-qt-libraries=${QT_LIBDIR} \
			--with-extra-libs=${LOCALBASE}/lib \
			--with-extra-includes=${LOCALBASE}/include

CONFIGURE_ENV+=	QTDIR="${QT_PREFIX}" QMAKE="${QMAKE}" \
		MOC="${MOC}" RCC="${RCC}" UIC="${UIC}" \
		QMAKESPEC="${QMAKESPEC}"

PLIST_SUB+=	QT_INCDIR=${QT_INCDIR_REL} \
		QT_LIBDIR=${QT_LIBDIR_REL} \
		QT_PLUGINDIR=${QT_PLUGINDIR_REL}

Some configure scripts do not support the arguments above. To suppress modification ofCONFIGURE_ENV and CONFIGURE_ARGS, set QT_NONSTANDARD.

6.12.2. Component Selection

Individual Qt tool and library dependencies must be specified in USE_QTx. Every component can be suffixed with _build or _run, the suffix indicating whether the dependency on the component is at buildtime or runtime. If unsuffixed, the component will be depended on at both build- and runtime. Usually, library components are specified unsuffixed, tool components are mostly specified with the _build suffix and plugin components are specified with the _run suffix. The most commonly used components are listed below (all available components are listed in _USE_QT_ALL, _USE_QT4_ONLY, and _USE_QT5_ONLY in /usr/ports/Mk/bsd.qt.mk):

表格 6.12. 可用的 Qt 函式庫元件
名字描述
core核心函式庫 (Qt 5 only)
corelib核心函式庫 (Qt 4 only)
dbusQt DBus 函式庫
gui圖形使用者介面函式庫
network網路函式庫
openglQt OpenGL 函式庫
scriptscript library
sqlSQL 函式庫
testlibunit testing library
webkitQt WebKit 函式庫
xmlQt XML 函式庫

To determine the libraries an application depends on, run ldd on the main executable after a successful compilation.

表格 6.13. Available Qt Tool Components
名字描述
qmakeMakefile generator/build utility
buildtoolsbuild tools (moc, rcc), needed for almost every Qt application (Qt 5 only)
linguisttoolslocalization tools: lrelease, lupdate (Qt 5 only)
linguistlocalization tools: lrelease, lupdate (Qt 4 only)
mocmeta object compiler, needed for almost every Qt application at buildtime (Qt 4 only)
rccresource compiler, needed if the application comes with *.rc or *.qrc files (Qt 4 only)
uicuser interface compiler, needed if the application comes with *.ui files, in practice, every Qt application with a GUI (Qt 4 only)

表格 6.14. Available Qt Plugin Components
名字描述
iconenginesSVG icon engine plugin, needed if the application ships SVG icons (Qt 4 only)
imageformatsplugins for TGA, TIFF, and MNG image formats

範例 6.5. Selecting Qt 4 Components

In this example, the ported application uses the Qt 4 graphical user interface library, the Qt 4 core library, all of the Qt 4 code generation tools and Qt 4's Makefile generator. Since the gui library implies a dependency on the core library, corelib does not need to be specified. The Qt 4 code generation tools moc, uic and rcc, as well as the Makefile generator qmake are only needed at buildtime, thus they are specified with the _build suffix:

USE_QT4=	gui moc_build qmake_build rcc_build uic_build

6.12.3. 使用 qmake

If the application provides a qmake project file (*.pro), define USES= qmake along with USE_QTx. Note that USES= qmake already implies a build dependency on qmake, therefore the qmake component can be omitted from USE_QTx. Similar to CMake, qmake supports out-of-source builds, which can be enabled by specifying the outsource argument (see USES= qmake example).

表格 6.15. Variables for Ports That Use qmake
VariableMeans
QMAKE_ARGSPort specific qmake flags to be passed to the qmake binary.
QMAKE_ENVEnvironment variables to be set for the qmake binary. The default is ${CONFIGURE_ENV}.
QMAKE_SOURCE_PATHPath to qmake project files (.pro). The default is ${WRKSRC} if an out-of-source build is requested, empty otherwise.

範例 6.6. USES= qmake 範例

This snippet demonstrates the use of qmake for a Qt 4 port:

USES=		qmake:outsource
USE_QT4=	moc_build

For a Qt 5 port:

USES=		qmake:outsource
USE_QT5=	buildtools_build

Qt applications are often written to be cross-platform and often X11/Unix is not the platform they are developed on, which in turn leads to certain loose ends, like:

  • Missing additional include paths. Many applications come with system tray icon support, but neglect to look for includes and/or libraries in the X11 directories. To add directories to qmake's include and library search paths via the command line, use:

    QMAKE_ARGS+=	INCLUDEPATH+=${LOCALBASE}/include \
    		LIBS+=-L${LOCALBASE}/lib
  • Bogus installation paths. Sometimes data such as icons or .desktop files are by default installed into directories which are not scanned by XDG-compatible applications. editors/texmaker is an example for this - look at patch-texmaker.pro in the files directory of that port for a template on how to remedy this directly in the qmake project file.

本文及其他文件,可由此下載: ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/

若有 FreeBSD 方面疑問,請先閱讀 FreeBSD 相關文件,如不能解決的話,再洽詢 <questions@FreeBSD.org>。

關於本文件的問題,請洽詢 <doc@FreeBSD.org>。