8000 DRAFT: Clazy qt6/qt5 recommended fixes by hjmjohnson · Pull Request #8543 · Slicer/Slicer · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@hjmjohnson
Copy link
Member

Work is being done to address Qt6 upgrade preparations by using the clazy compilation to expose recommended updates.

THIS PR IS NOT INTENDED TO BE MERGED. Items identified during this process should be "cherry-picked" and incorporated independently of this PR.

In many cases, small commits will be able to be re-arranged and merged, then cherry-picked to separate topics for merging.

@hjmjohnson hjmjohnson force-pushed the clazy-qt6-auto-fixes branch 3 times, most recently from bec7e01 to b2eda11 Compare July 12, 2025 16:04
@hjmjohnson hjmjohnson changed the title DRAFT: Clazy qt6 auto fixes DRAFT: Clazy qt6/qt5 recommended fixes Jul 12, 2025
@hjmjohnson hjmjohnson force-pushed the clazy-qt6-auto-fixes branch from b2eda11 to b2226af Compare July 13, 2025 14:49
@hjmjohnson hjmjohnson force-pushed the clazy-qt6-auto-fixes branch from b2226af to 6560581 Compare July 14, 2025 22:03
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.
Depends upon: Slicer/SlicerSurfaceToolbox#79

Need synchronize with SlicerSurfaceToolbox to include
support for finding .ui files with AUTOMOC.
Clazy can automatically fix many Qt5-to-Qt6 porting issues using Fix‑It
style rewrites—but with some important caveats. Here’s how it works:

Clazy includes several Qt6‑porting checks that provide automatic
fix-it suggestions, including:
  •  qt6-deprecated-api-fixes
  •  qt6-header-fixes
  •  qt6-qhash-signature
  •  qt6-fwd-fixes
  •  missing-qobject-macro
 
These checks can identify deprecated APIs and even offer automatic replacements.

Clazy generates .clazy.yaml files alongside your source files,
containing replacement instructions.
  •  Use clang-apply-replacements <path> to apply changes in bulk.
  •  Conflicts or ambiguous fix‑its are reported and halted.

⚠️ Limitations and Notes
  •  Not all Qt6 migration issues can be auto-fixed. Some require manual review.
  •  Fix‑its may conflict if multiple rules apply to the same code line. Handle with caution.
  •  After applying fixes, your code may only compile under Qt6, not
     with Qt5.  Use git diff tools to add pre-processor alternate
     compilations paths to support both Qt5 and Qt6
     #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
       menuOption.tabWidth = 0;
     #endif
Signals for view properties in qMRMLAbstractViewWidget

Added `viewLabelChanged` and `viewColorChanged` signals and
updated Q_PROPERTY macros with NOTIFY to enable property change
notifications.
Signals for view properties in qMRMLAbstractViewWidget

Added `viewLabelChanged` and `viewColorChanged` signals and
updated Q_PROPERTY macros with NOTIFY to enable property change
notifications.
@hjmjohnson hjmjohnson force-pushed the clazy-qt6-auto-fixes branch from 6560581 to ae6cd60 Compare July 14, 2025 22:07
@jcfr jcfr added the Status: Draft This pull-request is not yet ready for integration label Jul 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Status: Draft This pull-request is not yet ready for integration

Development

Successfully merging this pull request may close these issues.

2 participants

0