Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hjmjohnson
Copy link
Member

@hjmjohnson hjmjohnson commented Jul 14, 2025

ENH: Initial code changes to support qt5.15 and qt6

Add missing Q_OBJECT to complete Qt inheritance best practices

The Q_OBJECT macro is mandatory for any class that:
• Inherits from QObject
• Uses signals or slots
• Declares properties (Q_PROPERTY)
• Needs qobject_cast, metaObject(), or other Qt RTTI features

Without it:
• Qt’s meta-object compiler (moc) won’t generate the necessary code
• You may encounter runtime errors or silently broken signals/slots
• Clazy and Qt's build system may warn or fail

These best practices deficiencies were identified and auto-fixed with
the clazy tool (Qt adaptation of the clang-tidy tools).

This triggered conversion to AUTOMOC instead of trying to figure out
the necessary wrapping that would have been needed to support the
new Q_OBJECT infrastructures..

DEPENDS ON:

  • - ENH: Prepare for Qt6 build with AUTOMOC support SlicerSurfaceToolbox#79

  • COMP: Fix Qt linkage requirements

  • COMP: Add support for Qt5 & Qt6 signal/slot syntax

  • ENH: Enable Qt auto-tools in preparation for Qt6 compatibility

  • COMP: Change include for AUTOMOC naming conventions

  • COMP: Replace explicit Qt5 macros with CMake auto-tooling

  • COMP: Fix AUTOMOC for SlicerSurfaceToolbox

Prepares for FUTURE CHANGES FOR UPDATING CTK:

@hjmjohnson hjmjohnson marked this pull request as draft July 14, 2025 17:01
@hjmjohnson hjmjohnson changed the title enable automoc for qt wrapping COMP: Enable automoc for qt wrapping Jul 14, 2025
@hjmjohnson hjmjohnson self-assigned this Jul 14, 2025
@hjmjohnson hjmjohnson force-pushed the enable-automoc-for-qt-wrapping branch 3 times, most recently from 2757408 to 6b9fe7c Compare July 15, 2025 13:45
@hjmjohnson
Copy link
Member Author

@jcfr failures are due to SurfaceToolbox

AutoUic error
-------------
"SRC:-build/SurfaceToolbox/DynamicModeler/qSlicerDynamicModelerModuleWidget.cxx"
includes the uic file "ui_qSlicerDynamicModelerModuleWidget.h",
but the user interface file "qSlicerDynamicModelerModuleWidget.ui"
could not be found in the following directories

@hjmjohnson hjmjohnson force-pushed the enable-automoc-for-qt-wrapping branch 2 times, most recently from 411eb4a to 393b8fc Compare July 16, 2025 12:14
@hjmjohnson hjmjohnson force-pushed the enable-automoc-for-qt-wrapping branch from 393b8fc to 463c8a3 Compare August 27, 2025 14:25
Classes derived from Q_OBJECT can not be declared/defined
in an anonymous namespace because it affects qt's moc
to generate the neccessary items needed for linkage.

In Qt, when a class has the Q_OBJECT macro, it requires moc (Meta-Object
  Compiler) to generate code implementing functions like:
  •   metaObject() const
  •   qt_metacast()
  •   qt_metacall()
  •   signal/slot dispatch code

If moc is not run, or the result is not compiled/linked,
  you’ll get linker errors like the one above.

----
Followup fixes needed:

Even though event is declared as a QEvent*, the compiler cannot
guarantee that it actually points to a QMouseEvent. The classes aren’t
related through inheritance in a static type-safe way.

Casting across siblings in the event class hierarchy is invalid.

Use dynamic_cast<> for casting between heirchies that do *not*
  derive from QObject (i.e. QEvents do not derive from QObjects)

Use qobject_cast<> for casting between QObjects.
  Qt provides qobject_cast<T*>(QObject*) as a safe way to cast between
  QObject-derived types using Qt’s meta-object system (requires Q_OBJECT
  in the class).  It is RTTI‑free, works across shared libraries, and
  is available in both Qt 5 and Qt 6   .
    •   The function is declared in qobject.h, which is indirectly
        included by <QObject> in both Qt versions
Add missing Q_OBJECT to complete Qt inheritance best practices

The Q_OBJECT macro is mandatory for any class that:
   • Inherits from QObject
   • Uses signals or slots
   • Declares properties (Q_PROPERTY)
   • Needs qobject_cast, metaObject(), or other Qt RTTI features

Without it:
   • Qt’s meta-object compiler (moc) won’t generate necessary code
   • You may encounter runtime errors or silently broken signals/slots
   • Clazy and Qt's build system may warn or fail

These best practices deficiencies were identified and auto-fixed with
the clazy tool (Qt adaptation of the clang-tidy tools).
Preparing for Qt6 where the CMakeified auto-tooling
is preferred over the explicit wrapping.
The manual QT5 wrapping macros used moc_{class}.cxx for naming
of autogenerated files.  AUTOMOC uses {class}.moc as the
naming for the the autogenerated files.

Adding missing {class}.moc includes for the AUTOMOC behaviors.
Refactored CMakeLists to utilize lists and definitions for MOC
and resource files instead of explicit `QT5_WRAP_CPP`, `QT5_WRAP_UI`,
and `QT5_GENERATE_MOCS`. This change improves compatibility with
both Qt5 and Qt6 by adhering to modern CMake standards.
Need synchronize with SlicerSurfaceToolbox to include
support for finding .ui files with AUTOMOC.
@hjmjohnson hjmjohnson force-pushed the enable-automoc-for-qt-wrapping branch from 463c8a3 to 9e65669 Compare August 28, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants