在ADTF 3.3中集成Qt开发时,能否通过CMake在构建阶段自动生成MOC与UIC文件?
Absolutely! You don’t have to manually generate UIC headers or MOC files anymore—ADTF’s build system fully supports CMake’s automatic Qt code generation features. Here’s how to set it up correctly:
Step-by-Step Configuration
Enable CMake’s Qt Auto-Features First
Before calling ADTF’s Qt helper macro, turn on the automatic generation flags in yourCMakeLists.txt. This tells CMake to handle UIC, MOC, and RCC processes during the build:set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON)Make sure you’re using CMake 3.14 or newer—these auto-features are more reliable and better integrated with Qt in newer CMake versions.
Integrate ADTF’s Qt Support
Next, include ADTF’s CMake macros and calladtf_use_qt()with the Qt modules you need (likeWidgetsin your case). You don’t need a separatefind_package(Qt5)call, sinceadtf_use_qt()handles that for you:find_package(ADTF REQUIRED COMPONENTS filtersdk ui) include(${ADTF_CMAKE_DIR}/macros/adtf_use_qt.cmake) adtf_use_qt(Widgets)Add Your Source Files (Including UI Files)
When defining your ADTF filter or application, add your raw UI files, header files (withQ_OBJECTmacros), and source files directly to theadtf_add_filteroradd_executablecommand. CMake will automatically trigger UIC to generate headers from.uifiles, and MOC to generate meta-object code for classes withQ_OBJECT:adtf_add_filter(your_qt_filter src/your_filter.h src/your_filter.cpp ui/your_form.ui # No need to add manually generated .h files! )Fix Include Paths (If Needed)
If your UI files or headers are stored in non-standard directories, tell CMake where to find them so the auto-generated files can reference your code correctly:target_include_directories(your_qt_filter PRIVATE src ui )
Why This Works (And Why You Had to Do It Manually Before)
The issue in your original setup was likely that you enabled ADTF’s Qt support before turning on CMake’s auto-features, or you weren’t including the raw .ui/header files in your target’s source list. ADTF’s adtf_use_qt() macro doesn’t disable CMake’s auto-generation—it just sets up the necessary Qt dependencies and ADTF-specific Qt integration.
Key Notes to Avoid Issues
- Always declare
Q_OBJECT: Any class using Qt signals/slots must inherit fromQObject(or a subclass likeQWidget) and include theQ_OBJECTmacro in its header. This is how MOC knows which classes to process. - No manual files in source lists: Remove any manually generated
.h(from UIC) ormoc_*.cppfiles from your target’s source list—CMake will handle these automatically and avoid conflicts. - Add extra Qt modules if needed: If you need additional Qt modules (like
CoreorMultimedia), just add them to theadtf_use_qt()call, e.g.,adtf_use_qt(Widgets Core Multimedia).
内容的提问来源于stack exchange,提问作者user12071441

