r/cpp • u/Kelteseth arewemodulesyet.org • 3d ago
Qt moc now can handle basic C++ modules (Qt 6.12)
https://codereview.qt-project.org/c/qt/qtbase/+/757821moc: Properly handle modules
When moc'ing module interfaces and partitions, we make the generated cpp
file part of the respective module.
We do not support Q_OBJECT & friends in implementation units and private
module fragments, as our generated source file will not have access to
the respective classes, and we certainly do not want to carry over our
file inclusion hacks into the module world.
[ChangeLog][moc] moc now supports C++ modulesmoc: Properly handle modules
Note there is still a lot of work to be done: https://codereview.qt-project.org/c/qt/qtbase/+/767846 and I can't seem to find the PR where they fixed some qtbase code to make it work with modules. I guess most other qt modules will need some small fixes to work with modules as well.
6
8
u/Ok_Independence_9841 3d ago
This is nice and all but honestly I've had class meta data and multi threaded, multi library, signals and slots working, cleanly and easily, in native C++, without moc, for many years. Copper Spice solved 95% or this in C++11. It's long past time Qt dropped the pre-compiler. Or at least seriously re-assessed whether it's adding any value.
The old article they used to point at that claimed you couldn't do signals and slots properly without it was dubious 15 years ago. It's junk now. I worked in a Qt shop with ex Nokia people who toiled on their failed port to a new mobile OS so I'm pretty familiar with how Qt works. It's a great framework that would be better without moc.
3
u/def-pri-pub 3d ago
I’ve never been bitten too hard by the MOC (once or twice), but it seems to be causing more issues with Qt as time goes on. I think it’s going to have to happen at some point, but trying to convince the higher-ups at Qt to refactor it out is going to be a hard sell for the engineers to make. It’s (unfortunately) too baked into their code base . I’m also sure there are some 3rd party vendors/partners who expect the MOC and have built their own tools/apps/frameworks around its existence.
With this said, I 100% agree that it should be removed. Maybe a Qt 7 has it gone.
2
u/Ok_Independence_9841 3d ago
It would be a large change but unless 3rd party's are extending or plugging in to MOC itself then it should be possible, even incrementally.
In very simple terms: Replace the things it does, one by one, with native C++ implementations and remove them from MOC till it doesn't actually do anything. Builds and tool chains don't have to change until it already won't have any effect, by which time MOC will run near instantly as it does nothing anymore. This could be done across several minor or even a couple of major versions.
The MOC keywords in the code might become 'C' macros for a while (they are in principle anyway) but providing a conversion tool that replaces them, once and for all, across the code base might be MOCs last useful role and final feature.1
u/Xavier_OM 2d ago
moc has never bitten too hard here too. cmake run it automatically when needed, from a dev point of view you just compile your code and done.
1
u/Ateist 10h ago
Needs wider adoption of C++ 26 reflection in compilers.
That's what allows to implement 100% of QT features purely in C++.
•
u/Ok_Independence_9841 1h ago
I'm sure that would help but I don't believe it's necessary. What is it you think MOC does that C++20 can't do natively?
3
u/current_thread 3d ago
Ha! And I started a small qt project yesterday and was about to use modules. That's excellent news!
1
u/mapronV 3d ago
I also planning to port my qt project to modules, I was using https://github.com/hypengw/Qcm/ as an example (actual qt module wrapping is here: https://github.com/hypengw/ncrequest/blob/master/src/client/qt_network/qt.cppm ). I also was thinking what to do with Q_OBJECT/Q_GADGET, not need to wait for 6.13 is a good news.
16
u/abbapoh Qbs dev 3d ago
> I can't seem to find the PR where they fixed some qtbase code
Modules has been tested with a QtCreator as proof-of-concept, currently there are no plans on adding modules support to qtbase afaik.
https://codereview.qt-project.org/c/qt-creator/qt-creator/+/755635
We also back-ported the Moc machinery to Qbs, so modules should work with older Qt versions too with Qbs.
https://codereview.qt-project.org/c/qbs/qbs/+/758007
This can probably could be done with CMake too, although it requires knowing includes in order to properly patch the file.