Qt5 模組

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

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 布林值: 自 1.4.0 起。如果為 true,則指定輸出檔案需要在目標暫存目錄中維持其目錄結構。例如,當處理名為 subdir/one.input 的檔案時,當為 true 時會產生檔案 {target private directory}/subdir/one.out,而當為 false 時會產生 {target private directory}/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: 其 include 目錄由 moc 使用的相依性物件。
  • include_directories (字串 | IncludeDirectory)[]: 轉譯 .moc 檔案時使用的 include_directory() 物件清單
  • preserve_paths 布林值: 1.4.0 新增。如果為 true,則指定輸出檔案需要在目標暫存目錄中維持其目錄結構。例如,當處理名為 subdir/one.input 的檔案時,當為 true 時會產生檔案 {target private directory}/subdir/one.out,而當為 false 時會產生 {target private directory}/one.out (預設值)。

preprocess

請考慮改用 compile_resourcescompile_uicompile_moc

取得 moc、uic 和 rcc 的來源,並將它們轉換為 C++ 檔案以進行編譯。

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

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

自 Meson 0.59.0 以來,可變的 sources 引數和 sources 關鍵字引數已被棄用。這些未經修改地通過預處理器傳遞的檔案,請勿這樣做,只需將產生器的輸出新增到另一個來源清單即可

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 的任何其他引數。自 v0.44.0 起。
  • uic_extra_arguments 字串[]: uic 的任何其他引數。自 v0.49.0 起。
  • rcc_extra_arguments 字串[]: rcc 的任何其他引數。自 v0.49.0 起。
  • dependencies Dependency[]: moc 所需的相依性物件。自 v0.48.0 起可用。
  • sources: 額外來源的清單,它們在輸出中保持不變。在 0.59.0 中已棄用。
  • preserve_paths 布林值: 1.4.0 新增。如果為 true,則指定輸出檔案需要在目標暫存目錄中維持其目錄結構。例如,當處理名為 subdir/one.input 的檔案時,當為 true 時會產生檔案 {target private directory}/subdir/one.out,而當為 false 時會產生 {target private directory}/one.out (預設值)。

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

compile_translations

自 0.44.0 起

此方法會產生必要的目標,以使用 lrelease 建構翻譯檔案,它不接受位置引數,但接受以下關鍵字引數

  • ts_files (檔案 | 字串 | custom_target | custom_target 索引 | generator_output)[]: Qt 的 lupdate 工具產生的輸入翻譯檔案清單。0.60.0 新增: 支援 custom_target、custom_target_index 和 generator_output。
  • install 布林值: 如果為 true,則會在安裝步驟期間安裝此目標(選用)。
  • install_dir 字串: 要安裝到的目錄(選用)。
  • build_by_default 布林值: 如果設定為 true,則預設會建構此目標,也就是說,在調用 meson compile 時;預設值為 false(選用)。
  • qresource 字串: 要從中提取 ts_files 的 rcc 來源檔案;不能與 ts_files kwarg 一起使用。自 v0.56.0 起可用。
  • rcc_extra_arguments 字串[]: rcc 的任何其他引數(選用),與 `qresource 一起使用時可用。自 v0.56.0 起可用。

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

has_tools

自 0.54.0 起

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

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

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

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

  • required 布林值 | FeatureOption: 預設情況下,required 設定為 false。如果 required 設定為 true 或啟用了 feature,並且缺少某些工具,則 Meson 將中止。
  • method 字串: 用於尋找 Qt 相依性的方法(預設為 auto)。

相依性

請參閱 Qt 相依性

'modules' 引數用於在專案中包含 Qt 模組。有關模組清單,請參閱 Qt 文件。

'private_headers' 引數允許使用 Qt 模組的私有標頭。(自 v0.47.0 起)

範例

一個簡單的範例看起來像這樣

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

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

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

搜尋結果為