Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. About #include directive
Forum Updated to NodeBB v4.3 + New Features

About #include directive

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patrick Latour
    wrote on last edited by
    #1

    Hello there and sorry if my question look too simple or if I am at the wrong place to ask. I develop since a long time, but I am pretty new to c++ and even newer to Qt (a great tool by the way).

    So here is the question: When I look about the Modules documentation, network module to be more accurate, at the end it says:
    "To include the definitions of the module's classes, use the following directive: #include <QtNetwork>"
    No problem so far and I understand the general idea behind the include. However, when I look a class documentation in the network module, let's say: QHostAddress, at the beginning it says:"The QHostAddress class provides an IP address. #include <QHostAddress>"

    Now I am confused, do I have to include both ? If I include only the Module, does it include automatically all the classes in this module? Do I include only the class? Does it have an impact on the compiled (executable) file, like size or speed?

    Sorry again if it looks too simple for you, but like I said, I am pretty new to c++ and Qt.

    Thanks in advance for your help.

    Patrick

    Make it idiot proof and someone will make a better idiot

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fluca1978
      wrote on last edited by
      #2

      The rule is that each class has an include with the same name. So if you need class X you have to include file X. For convenience and to avoid a long list of includes in your code, you can include a few "special" includes that do the inclusions of each the classes for you. If you include both the includes are enough smart (i.e., they have macros) to prevent conflicts or double inclusion.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        The "QtModule" includes will include all the headers in that module, so you don't need to include a particolar header again. The only impact they have is on compilation time: dozens of headers need to be opened, read, parsed, even if you're only using a small subset of them.

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Patrick Latour
          wrote on last edited by
          #4

          Wow fast answer. Thanks guys, things are more clearer now. Your help is really appreciated.

          Make it idiot proof and someone will make a better idiot

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            I'll kind of steal your topic, if I may, Patrick. Please?

            OK, what I am, and always have been, curious about, are the "nested" includes like:
            @
            #include <QtCore/QString>
            @

            Are they any different from simple:
            @
            #include <QString>
            @
            ?

            (Z(:^

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              At the moment: no.
              But from Qt5, I believe the first version will become the standard. The idea is that we will have a much more modularized Qt, and you will see that reflected in the included. I now use the first form for new code.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                thanks a lot, that's one mystery less on my list :)

                (Z(:^

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  There's a little difference :-)

                  With the first form

                  @
                  #include <QtCore/QString>
                  @

                  you only need one directory in the include path, e.g.

                  @
                  INCLUDEPATHS += /usr/local/Trolltech/Qt-4.7.4/include
                  @

                  As you add the module's subdirectory in the #include directive, it will find every include file.

                  With the second form

                  @
                  #include <QString>
                  @

                  you will explicitly have to add the QtCore submodule in your include path:

                  @
                  INCLUDEPATHS += /usr/local/Trolltech/Qt-4.7.4/include/QtCore
                  @

                  While this is done automatically when using qmake, it might be of interest when setting up a build environment manually.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    I see. Thanks again, this time to Volker :) Couldn't find a decent explanation for ages.

                    (Z(:^

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Patrick Latour
                      wrote on last edited by
                      #10

                      No problem to stealing my post, as long as it is profitable for all, and after reading your question, yes it is profitable. It is a good point and thanks to Andre and Volker for their answer. I will go with the first from from now on, to avoid a lot of corrections in the future when porting applications to Qt5
                      [quote author="sierdzio" date="1323324830"]I'll kind of steal your topic, if I may, Patrick. Please?

                      OK, what I am, and always have been, curious about, are the "nested" includes like:
                      @
                      #include <QtCore/QString>
                      @

                      Are they any different from simple:
                      @
                      #include <QString>
                      @
                      ?[/quote]

                      Make it idiot proof and someone will make a better idiot

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved