Qt6 模組

Meson 0.57.0 新增功能

Qt6 模組提供自動處理 Qt 所需各種工具和步驟的方法。

警告: 在 0.63.0 版本之前,由於 Qt 工具已移至 libexec 子目錄,且工具名稱僅以 Qt 主要版本號為後綴(例如 qmake6),Meson 無法找到 Qt 6.1 或更高版本。

compile_resources

0.59.0 版本新增

將 Qt 的資源集合檔案 (.qrc) 編譯成用於編譯的 c++ 檔案。

它不接受位置引數,並接受以下關鍵字引數

  • name (字串 | 空):如果提供,將生成單個 .cpp 檔案,並且所有 qrc 檔案的輸出將合併在此檔案中,否則每個 qrc 檔案將寫入其自己的 cpp 檔案。
  • sources (檔案 | 字串 | custom_target | custom_target 索引 | generator_output)[]:要轉譯的來源清單。 必需,必須至少有一個來源
    0.60.0 版本新增:支援 custom_target、custom_target_index 和 generator_output。
  • extra_args 字串[]:直接傳遞給 qt-rcc 的額外引數
  • method 字串:用於偵測 Qt 的方法,請參閱 dependency()

compile_ui

0.59.0 版本新增

將 Qt 的 ui 檔案 (.ui) 編譯成標頭檔。

它不接受位置引數,並接受以下關鍵字引數

  • sources (檔案 | 字串 | custom_target | custom_target 索引 | generator_output)[]:要轉譯的來源清單。 必需,必須至少有一個來源
    0.60.0 版本新增:支援 custom_target、custom_target_index 和 generator_output。
  • extra_args 字串[]:直接傳遞給 qt-uic 的額外引數
  • method 字串:用於偵測 Qt 的方法,請參閱 dependency()
  • preserve_paths bool: 1.4.0 版本新增。 如果為 true,則指定輸出檔案需要在目標暫存目錄中維護其目錄結構。 例如,當處理名為 subdir/one.input 的檔案時,如果為 true,則會生成檔案 {目標私有目錄}/subdir/one.out,如果為 false(預設),則會生成檔案 {目標私有目錄}/one.out

compile_moc

0.59.0 版本新增

將 Qt 的 moc 檔案 (.moc) 編譯成標頭檔和/或原始碼檔案。 必須提供關鍵字引數 headerssources 至少其中一個。

它不接受位置引數,並接受以下關鍵字引數

  • sources (檔案 | 字串 | custom_target | custom_target 索引 | generator_output)[]:要轉譯為 .moc 檔案以供手動包含的來源清單。
    0.60.0 版本新增:支援 custom_target、custom_target_index 和 generator_output。
  • headers (檔案 | 字串 | custom_target | custom_target 索引 | generator_output)[]:要轉譯為 .cpp 檔案的標頭清單
    0.60.0 版本新增:支援 custom_target、custom_target_index 和 generator_output。
  • extra_args 字串[]:直接傳遞給 qt-moc 的額外引數
  • method 字串:用於偵測 Qt 的方法,請參閱 dependency()
  • dependencies:其包含目錄由 moc 使用的相依性物件。
  • include_directories (字串 | IncludeDirectory)[]:轉譯 .moc 檔案時使用的 include_directory() 物件清單
  • preserve_paths bool: 1.4.0 版本新增。 如果為 true,則指定輸出檔案需要在目標暫存目錄中維護其目錄結構。 例如,當處理名為 subdir/one.input 的檔案時,如果為 true,則會生成檔案 {目標私有目錄}/subdir/one.out,如果為 false(預設),則會生成檔案 {目標私有目錄}/one.out

preprocess

請考慮改用 compile_resourcescompile_uicompile_moc

取得 moc、uic 和 rcc 的來源,並將其轉換為用於編譯的 c++ 檔案。

具有以下簽名:qt.preprocess(name: str | None, *sources: str)

如果傳遞了 name 參數,則所有 rcc 檔案都將寫入單個輸出檔案

在 0.59.0 版本中已棄用:在可變引數 sources 中以及 sources 關鍵字引數中給出的檔案,會未經修改地傳遞給前置處理器程式。 請勿這樣做 - 僅將 preprocess() 的輸出新增到另一個來源清單

sources = files('a.cpp', 'main.cpp', 'bar.c')
sources += qt.preprocess(qresources : ['resources'])

此方法採用以下關鍵字引數

  • qresources (字串 | 檔案)[]:傳遞給 RCC 編譯器
  • ui_files:(字串 | 檔案 | CustomTarget)[]:傳遞給 uic 編譯器
  • moc_sources:(字串 | 檔案 | CustomTarget)[]:傳遞給 moc 編譯器。 這些會轉換為旨在被 #include 的 .moc 檔案
  • moc_headers:(字串 | 檔案 | CustomTarget)[]:傳遞給 moc 編譯器。 這些將轉換為 .cpp 檔案
  • include_directories (IncludeDirectories | 字串)[],要新增到 moc 的標頭搜尋路徑的目錄
  • moc_extra_arguments 字串[]:moc 的任何其他引數。
  • uic_extra_arguments 字串[]:uic 的任何其他引數。
  • rcc_extra_arguments 字串[]:rcc 的任何其他引數。
  • dependencies Dependency[]:moc 所需的相依性物件。
  • 在 0.59.0 版本中已棄用。sources:額外來源的清單,它們會未經更改地新增到輸出中。
  • preserve_paths bool:自 1.4.0 版本起。 如果為 true,則指定輸出檔案需要在目標暫存目錄中維護其目錄結構。 例如,當處理名為 subdir/one.input 的檔案時,如果為 true,則會生成檔案 {目標私有目錄}/subdir/one.out,如果為 false(預設),則會生成檔案 {目標私有目錄}/one.out

它會傳回要傳遞給編譯目標的目標和來源陣列。

compile_translations

此方法會生成使用 lrelease 建置翻譯檔案所需的目標,它不接受位置引數,並接受以下關鍵字引數

  • ts_files (檔案 | 字串 | custom_target | custom_target 索引 | generator_output)[]:Qt 的 lupdate 工具產生的輸入翻譯檔案清單。
    0.60.0 版本新增:支援 custom_target、custom_target_index 和 generator_output。
  • install bool:當為 true 時,此目標會在安裝步驟期間安裝(選用)。
  • install_dir 字串:要安裝到的目錄(選用)。
  • build_by_default bool:當設定為 true 時,讓此目標預設建置,也就是在呼叫 meson compile 時;預設值為 false(選用)。
  • qresource 字串:要從中提取 ts_files 的 rcc 原始碼檔案;不能與 ts_files kwarg 一起使用。
  • rcc_extra_arguments 字串[]:rcc 的任何其他引數(選用),與 `qresource` 一起使用時。

傳回以下其中之一:已編譯翻譯的自訂目標清單,或者,如果使用 qresource 檔案,則傳回包含已處理來源檔案的單個自訂目標,該檔案應傳遞給主要建置目標。

has_tools

如果找到此模組使用的所有工具,則此方法傳回 true,否則傳回 false

它應該用於編譯選用的 Qt 程式碼

qt6 = import('qt6')
if qt6.has_tools(required: get_option('qt_feature'))
  moc_files = qt6.preprocess(...)
  ...
endif

此方法採用以下關鍵字引數

  • required bool | FeatureOption:預設情況下,required 設定為 false。 如果 required 設定為 true 或已啟用的 feature 並且遺失某些工具,Meson 將中止。
  • method 字串:用於偵測 Qt 的方法,請參閱 dependency()
  • tools:字串[]:自 1.6.0 版本起。 要檢查的工具清單。 可測試的工具為 mocuicrcclrelease。 預設情況下,tools 設定為 ['moc', 'uic', 'rcc', 'lrelease']

相依性

請參閱 Qt 相依性

「modules」引數用於在專案中包含 Qt 模組。 請參閱 Qt 文件以取得 模組清單

「private_headers」引數允許使用 Qt 模組的私有標頭。

範例

一個簡單的範例如下所示

qt6 = import('qt6')
qt6_dep = dependency('qt6', modules: ['Core', 'Gui'])
inc = include_directories('includes')
moc_files = qt6.compile_moc(headers : 'myclass.h',
                            extra_arguments: ['-DMAKES_MY_MOC_HEADER_COMPILE'],
                            include_directories: inc,
                            dependencies: qt6_dep)
translations = qt6.compile_translations(ts_files : 'myTranslation_fr.ts', build_by_default : true)
executable('myprog', 'main.cpp', 'myclass.cpp', moc_files,
           include_directories: inc,
           dependencies : qt6_dep)

有時,翻譯會使用 qresource 檔案內嵌在二進位檔中。 在這種情況下,不需要明確列出 ts 檔案,而是會從 qresource 檔案中列出的已建置 qm 檔案中推斷出來。 例如

qt6 = import('qt6')
qt6_dep = dependency('qt6', modules: ['Core', 'Gui'])
lang_cpp = qt6.compile_translations(qresource: 'lang.qrc')
executable('myprog', 'main.cpp', lang_cpp,
           dependencies: qt6_dep)

搜尋結果為