5

I have a project in QT Creator and I want that the header files be included automatically when using a new object. It's like in Eclipse : Add needed headers when invoking a new object by using Ctrl+Alt+Space : all needed headers will be included.

Here is an example

#include <QApplication>
#include <QPushButton>  /* Header that i want to include automatically */

 int main( int argc, char *argv[ ] )
{
    QApplication app( argc, argv) ;
    QWidget fenetre;
/*Add needed headers when invoking a new object like ctrl+alt+space in eclipse*/
    QPushButton bouton( "Bonjour", &fenetre) ;
    bouton. move(70,60);
    fenetre. show( ) ;
    return app. exec( ) ;
}

Any suggestions on how to do this in QT Creator ? Thanks

Alexander V
  • 8,351
  • 4
  • 38
  • 47
The Beast
  • 1,629
  • 2
  • 29
  • 42
  • 1
    No, but if you want to include a larger subset (almost everything) you can try for something like, #include and #include for example – Son-Huy Pham Jan 20 '16 at 21:10
  • Yes it possible but not the solution if there is a lot of lines of code !!! – The Beast Jan 20 '16 at 22:50
  • 1
    "Yes it possible but not the solution if there is a lot of lines of code !!!" It doesn't really matter in practice unless you *project* has many files. Personally I find that for small projects (<100 files) you're perfectly fine including everything. But even for large projects (1000s of files), precompiled headers make all the difference: use them! They'll make including the entire Qt modules have essentially zero cost. – Kuba hasn't forgotten Monica Jan 21 '16 at 14:51
  • OTOH, you must be dealing with really trivial code if manually typing this out makes any real difference. Suppose you have a "typical" source module between 1k-5k lines. What's a dozen or two include lines? Noise. – Kuba hasn't forgotten Monica Jan 21 '16 at 14:53
  • Anyone who has developed for some time in Java knows that this is gold, and excluding this feature from QtCreator is crippling at best. Another similar feature is that the needed imports are carried with any code you copy and paste between files so that you don't have to do that manually – Mr. Developerdude Mar 13 '16 at 12:00

2 Answers2

5

In QT Creator version 4.1.0 you have option for that under right click (move your cursor over type you want to include, click, choose refactor and there it is)

shortcut for that is (Alt+Return) to trigger quick fix , you can see what is the shortcut for it if it does not work on your machine Tools -> Options -> Environment -> Keyboard (Search for quickfix).

Awad
  • 3
  • 4
sebekk23
  • 51
  • 1
  • 1
2

Not exactly as you requested, but: if you would like to create a new class with the help of Qt Creator you can certainly have Qt Creator taking care of immediate include files as well:

enter image description here

Qt Documentation: Creating Projects.

Alexander V
  • 8,351
  • 4
  • 38
  • 47