How to customise Qt, so it add certain functions in every new cppf file?

强颜欢笑 提交于 2019-12-12 04:47:45

问题


I am a newbie to Qt, and I was wondering if it was possible to automatically add certain functions declaration in .h file and definition in .cpp file, whenever we add new c++ source file. For ex, if I add new cpp source file(cpp + h file), Qt should add automatically add function initialiseApp(), its declaration in h file and its definition in cpp file. This should also work if I create new project. This makes my work easy as I have to add certain functions in all cpp files. Thanks :)

EDIT: Sorry for the confusion I created. I meant Qt Creator(The Editor) I just want the editor to add custom functions whenever I add new C++ source file. For Ex, whenever i add new c++ source file it should add a function called initialiseObjects() in header file(its declaration) and in cpp file(just its definition). I know that refractor option exists but i am just too lazy to type the declaration in every new cpp header file and right click on to it to add definition. Once again Sorry for the confusion created


回答1:


Unless you just want templates to your editor (like GeneCode answered) - and then your question is unrelated to Qt as a library and depends much more on your source code editor - it is not easily possible. Some editors (notably GNU emacs) are easily customizable with a powerful scripting language (Elisp for emacs). Other editors (vim, gedit) are extensible, notably thru plugins. YMMV.

You could configure your build automation tool to do additional stuff (it is easy to add a few commands and rules in your Makefile). You could write some scripts or programs to generate initial source code content. You might write your own code preprocessor (or use some additional one, like GPP) or write and use some program or script generating C++ code. You could customize your compiler (e.g. developing your own GCC MELT extensions).

But there is no general way, and your question is too broad, and there are cases where you don't want any automation.

Notice however that in some cases you want some of your C++ code to be generated by something else. That idea is quite common. Qt has moc.




回答2:


Qt is a C++ framework. I'd assume you're talking about Qt Creator. In Creator this feature already is there. You can declare a function in .h and then right click on it and do Refactor => Add Definition in MyClass.cpp.




回答3:


Just go to your folder:

{Path to Qt installation}\Tools\QtCreator\share\qtcreator\templates

In here you can find all default templates and modify them there.



来源:https://stackoverflow.com/questions/46557223/how-to-customise-qt-so-it-add-certain-functions-in-every-new-cppf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!