PthinKS PthinKS Developer Forum forum.pthinks.com C++ • Qt • QML Engineering Community
PthinKS Engineering Q&A
forum.pthinks.com
Close
QML

How can the same constants be shared between C++ and QML?

↗ Original Qt Forum discussion

A developer wanted one definition for many constants used from both C++ and QML.

0 votes

1 Answer

DP
Dheerendra V Purohit
September 11, 2026 • Independent answer
✓ Accepted
Dheerendra noted that the shared values need a type visible to QML and suggested Q_GADGET as an option when QObject inheritance is unnecessary.
cpp
class Constants { Q_GADGET public: enum Value { MinValue=0, MaxValue=100 }; Q_ENUM(Value) };
0 replies
Continue the discussion under Dheerendra V Purohit's answer. This reply remains inside this answer thread.
Optional reply code

Answer this Question

Each person can post an independent answer. To discuss a specific answer, use Reply to this answer under that answer.

Explain your own solution first; add reusable code if needed.

Optional code solution