From e3c02573b4fb0e31998507d5bc0c47faf8819d9c Mon Sep 17 00:00:00 2001 From: Rafael Palomar Date: Fri, 6 Feb 2026 10:11:11 +0100 Subject: [PATCH] ENH: Modernize Qt integration in LayerDM tests - Replace manual QT5_GENERATE_MOCS with AUTOMOC - Use find_package with Qt version detection (Qt5/Qt6) - Simplify target linking with loop over Qt components - Update moc include to use .moc extension convention - Remove explicit Qt component dependencies in favor of dynamic linking This change makes the test configuration more maintainable and prepares the module for Qt6 compatibility. --- LayerDM/Testing/Cxx/CMakeLists.txt | 24 +++++++++++++++---- .../Testing/Cxx/NodeReferenceObserverTest.cxx | 3 ++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/LayerDM/Testing/Cxx/CMakeLists.txt b/LayerDM/Testing/Cxx/CMakeLists.txt index 09c55cd..44af87b 100644 --- a/LayerDM/Testing/Cxx/CMakeLists.txt +++ b/LayerDM/Testing/Cxx/CMakeLists.txt @@ -1,5 +1,11 @@ set(KIT ${MODULE_NAME}) -find_package(Qt5 REQUIRED COMPONENTS Core Widgets Test) + +list(APPEND QT_REQUIRED_COMPONENTS Core Test) +if(Slicer_REQUIRED_QT_VERSION VERSION_GREATER_EQUAL "6") + find_package(Qt6 COMPONENTS ${QT_REQUIRED_COMPONENTS} QUIET) +else() + find_package(Qt5 COMPONENTS ${QT_REQUIRED_COMPONENTS} QUIET) +endif() set(TEST_SOURCES NodeReferenceObserverTest.cxx @@ -11,16 +17,24 @@ create_test_sourcelist(Tests ${KIT}CppTests.cxx EXTRA_INCLUDE ${EXTRA_INCLUDE} ) -QT5_GENERATE_MOCS(${TEST_SOURCES} USE_MOC_EXTENSION) include_directories(${CMAKE_CURRENT_BINARY_DIR}) ctk_add_executable_utf8(${KIT}CxxTests ${Tests} ${Tests_MOC_CXX} ${Tests_UtilityFiles}) +set_target_properties(${KIT}CxxTests PROPERTIES + AUTOMOC ON +) + +foreach(lib IN LISTS QT_REQUIRED_COMPONENTS) + if(Slicer_REQUIRED_QT_VERSION VERSION_GREATER_EQUAL "6") + list(APPEND MODULE_TARGET_LIBRARIES Qt6::${lib}) + else() + list(APPEND MODULE_TARGET_LIBRARIES Qt5::${lib}) + endif() +endforeach() + target_link_libraries( ${KIT}CxxTests ${MODULE_TARGET_LIBRARIES} - Qt5::Core - Qt5::Test - Qt5::Widgets ) include(SlicerMacroSimpleTest) diff --git a/LayerDM/Testing/Cxx/NodeReferenceObserverTest.cxx b/LayerDM/Testing/Cxx/NodeReferenceObserverTest.cxx index e4c8548..bfa5699 100644 --- a/LayerDM/Testing/Cxx/NodeReferenceObserverTest.cxx +++ b/LayerDM/Testing/Cxx/NodeReferenceObserverTest.cxx @@ -249,4 +249,5 @@ private slots: }; CTK_TEST_MAIN(NodeReferenceObserverTest) -#include "moc_NodeReferenceObserverTest.cxx" + +#include "NodeReferenceObserverTest.moc"