Skip to content

Custom Qt MOC Build Rule

As I wrote in prevous post, it is possible to automate Visual Studio to use Qt MOC compiler in custom build rule. Visual studio can save build rules in a file and so we skip to type in them per every project. Rules can even be associated with a custom extension so that visual studio can apply them automatically.

To make it easy let’s use .hxx so that VS still recognizes it as a header file. From project properties, choose “Custom Build Rules …” ->“New Rule File ….” In next dialog fill in details for name and place to save it; for example “Qt MOC Compiler” for display name, Qt MOC as file name and choose some suitable directory for .rule file (just accept what VS suggests).

Observe – on Vista (and probably 7) there might be some problems in saving rule file in rule path, which by default points to VS installation directory, due to UAC. Instead choose some directory where you can write the file, and accept option to add that directory to rule path.

When rule is added, choose “Modify Rule File”->“Add Build Rule”. In dialog that opens, pretty much enter what you entered in Custom Build Rule for the header in Step 1, here is a screenshot:

When rule is made, we have also to enable it, by checking the checkbox next to its name in Custom Build Dialog. Unfortunately that has to be done once per project:

Once this is done, VS will automatically assing Qt MOC rule to .hxx headers. We can also keep header extensions to standard .h, or to something else. In that case we can choose properties for the header, and from list for cutom build tools, choose Qt MOC rule:

Unfortunately I don’t have any good way to automate step 2 and 3 (adding new files to project and compiling them by default).

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*