VS 2019 constantly changing Item Type of QT-class-containing header files
-
This is beyond frustrating.
In order to get the MOC to compile my QT-class-containing headers (.h), I need to right click on the headers and set their Item Type to 'Qt Meta-Object Compiler (moc)'. However, after changing a header's Item Type, if any compilation errors happens relating to that header during the next compile (could be anything...typo, bug, etc...not specifically related to the Qt class inside), the Item Type gets changed back to 'C/C++ header' and I have to manually switch the Item Type back.
I've tried toggling all relevant settings and I can't figure out what process is performing this shadow change. I have hundreds of Qt-class-containing headers and they're constantly getting their Item Type auto-switched back to 'C/C++ header', preventing further compiles until I switch them back to MOC.
I'm losing a lot of time and hair to this problem...any idea which process is swapping my Item Types without consent and how I can disable this insane, overstepping behavior?
-
Ok, so I actually figured this one out.
My initial suspicion that the header item type was being changed after a compilation error was incorrect....the header item type was actually being changed after every save of the header file (so, even worse than assumed!). In other words: set header item type to moc, make a change to the header, hit save...voila...header item type is reverted.
Turns out the cause is simple, but also annoying and probably something that should be addressed by the Qt devs:
If the header does not explicitly contain the macro 'Q_OBJECT' (outside of a comment block), then the header will be reverted from a moc item type to a c/c++ item type...and it was happening in my case due to the required qt classes being defined with a macro, instead of directly within the file (in other words, the Q_OBJECT macro was inside of a parent macro, defined in another file...and then brought into the various problematic header files with the parent macro). Ex (pseudocode):
parent.h: #define qtclass(x) class x{Q_OBJECT}; header.h: << gets reverted to 'c/c++ header' item type on save! #include "parent.h" qtclass(className)
The mere presence of the plaintext string Q_OBJECT (outside of a comment block) in my headers fixes the issue:
header.h: << no longer gets reverted with the below workaround! #include "parent.h" #ifdef Q_OBJECT //magic #endif qtclass(className)
So it seems some qt process is scanning files for 'Q_OBJECT' during file save and reverting their item type if that plaintext string is not found directly within the file in question.
Either way, Qt auto-modifying a user's item type assignment is absolutely inappropriate. If a file doesn't contain a Q_OBJECT macro and thus cannot be processed by the moc compiler, then print an error. DO NOT EVER secretly change a user's manual project settings!
-
Hi and welcome to devnet,
This is just an educated guess and by all mean not an explanation: this is done so to avoid useless file processing. There are other ways to trigger moc run, by for example, including the moc generated file at the bottom of the implementation file. This technique is used when you create a QObject based class with the macro in the implementation file.
That said, I understand that the behaviour is a bit surprising in your case but you are likely in a corner case that had not been encountered yet. It is likely something related to C++ macro expansion (but it is just a guess).
The best way you can help resolve the issue is to check the bug report system and open a ticket explaining what happens and provide a minimal compilable project showing your issue.