Qt without moc: set of macros to use Qt without needing moc
This (header-only) library can be used to create an application using Qt, without the need of the moc (MetaObject Compiler). It uses a different set of macro than Qt and templated constexpr code to generate the QMetaObject at compile-time. It is entirely binary compatible with Qt.
Blog post presenting the project: https://woboq.com/blog/verdigris-qt-without-moc.html (2016)
Blog post with some implementation details: https://woboq.com/blog/verdigris-implementation-tricks.html (2018)
Browse code online: https://code.woboq.org/woboq/verdigris
For an introduction, see the tutorial.cpp. See also a more detailed documetation of the macros in the source code.
Almost all features of Qt are working. The Qt test have been ported.
Features that are not yet working: - QPLUGINMETADATA: This would require compiling to the Qt's binary json. Out of scope for now. - QMetaMethod::tag(): Not yet implemented, could be supported if needed, but is not really needed for anything. (not even tested by Qt's auto test) - Q_ENUM: Working, but requires to repeat the name of every enum value. Could be improved.
New features compared to Qt with moc: - Support for templated QObject. - Support for QObject nested in another class.
The library consist of only two headers files. You can either copy these header files in your project, or adjust the include paths so that the compiler finds them. You will find the headers in the 'src/' sub-directory. Also make sure to set your compiler in, at least, C++14 mode. With qmake, you can do that with
CONFIG += c++14.
Tested with Qt >= 5.5. Need a compiler that can do C++14 relaxed constexpr such as GCC 5.1 or Clang 3.5, or MSVC 2017
lupdate, add the argument
-tr-function-alias Q_DECLARE_TR_FUNCTIONS+=W_OBJECTto avoid the warning that your class are not using the Q_OBJECT macro.
This table show the correspondence between Qt macro and Verdigris macro:
| Qt macro | Use the Verdigris macro instead | |----------------------------------------------|---------------------------------------------------| |
public slots: void mySlot(int x);|
void mySlot(int x); W_SLOT(mySlot)| |
signals: void mySignal(int x);|
void mySignal(int x) W_SIGNAL(mySignal,x)| |
Q_PROPERTY(int myProperty WRITE setProp READ getProp NOTIFY propChanged)|
W_PROPERTY(int, myProperty WRITE setProp READ getProp NOTIFY propChanged)| |
Q_INVOKABLE void myFunction(int foo);|
void myFunction(int foo); W_INVOKABLE(myFunction)|
Q_INVOKABLE MyClass(int foo);|
MyClass(int foo); W_CONSTRUCTOR(int)| |
W_CLASSINFO("foo", "bar")| |
W_NAMESPACE_IMPL(MyNs)| | QENUM/QFLAG/QENUMNS/QFLAGNS | WENUM/WFLAG/WENUMNS/WFLAGNS |
ISOTRONIC GmbH, a provider for visual inspection QA systems for automatic glass vial manufacturing, uses Verdigris in production:
The transition from standard Qt affected more than 100 source code files and was done in not much more than a day. The reason for the switch was to be able to use a build system that has no interface for Qt's MOC process. In the rare cases of questions or problems the Verdigris team was quick and competent in resolving the issues. After more than 6 months of real-world experience we are still very happy with that decision.
If you are using Verdigris and want to appear here, please open an issue, or a pull request
The macros were inspired by CopperSpice. The template code was based on previous work: https://woboq.com/blog/reflection-in-cpp-and-qt-moc.html
Differences with CopperSpice:
__LINE__in its macro making it impossible to declare several things in one line or to declare objects or properties from macros.
Like Qt, this library is under the dual licence LGPLv3 and GPLv2. Being header-only, this removes many of the obligations of the LGPLv3.
If you have any questions or remark please email [email protected]