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. QT_BEGIN_NAMESPACE
Forum Updated to NodeBB v4.3 + New Features

QT_BEGIN_NAMESPACE

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 13.6k Views 3 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.
  • G Offline
    G Offline
    Gian88
    wrote on last edited by Chris Kawa
    #1

    Hi, I have some dubts on the use of namespace.
    In an example I found this code for include this Class:

    QT_BEGIN_NAMESPACE
    
    class QLabel;
    
    class QPushButton;
    
    class QTextEdit;
    
    class QLineEdit;
    
    QT_END_NAMESPACE
    

    Then I tried to do the same thing, but didn't work,
    so I used

    #include <QPushButton> in place of
    QT_BEGIN_NAMESPACE
    
    class QPushButton;
    
    QT_END_NAMESPACE
    

    Do someone know why? THANKS.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bkerdev
      wrote on last edited by Chris Kawa
      #2

      Class.h

      QT_BEGIN_NAMESPACE
      
      class QLabel;
      class QPushButton;
      class QTextEdit;
      class QLineEdit;
      
      QT_END_NAMESPACE
      

      class.cpp

      #include <QPushButton>
      #include <QLabel>
      #include <QTextEdit>
      #include <QLineEdit>
      

      Boris Bker

      1 Reply Last reply
      0
      • JKSHJ Online
        JKSHJ Online
        JKSH
        Moderators
        wrote on last edited by
        #3

        @class QPushButton;@ is a forward declaration, which only tells the compiler what type QPushButton is, but doesn't tell the compiler what its methods/variables/etc. are.

        @#include <QPushButton>@ inserts the full header file of QPushButton into your code, so that the compiler knows what its methods/variables/etc. are.

        See http://stackoverflow.com/questions/553682/when-to-use-forward-declaration for tips on when to use forward declarations

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          For those who are interested in the meaning of "QT_BEGIN_NAMESPACE and QT_END_NAMESPACE, here":http://qt-project.org/wiki/Qt-In-Namespace are the details.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • T3STYT Offline
            T3STYT Offline
            T3STY
            wrote on last edited by T3STY
            #5

            Guys, I was wondering about using this little macro "trick". I have a namespace for most of my code too, but, when should I use a XXX_BEGIN_NAMESPACE ... XXX_END_NAMESPACE, and when is it more appropriate a using nampesace XXX ?
            My understanding is that the first one is more appropriate for header files (.h) where most of the classes and global object' declarations go; while in source code files (.cpp) the second is more appropriate. Do you agree?

            JKSHJ 1 Reply Last reply
            0
            • T3STYT T3STY

              Guys, I was wondering about using this little macro "trick". I have a namespace for most of my code too, but, when should I use a XXX_BEGIN_NAMESPACE ... XXX_END_NAMESPACE, and when is it more appropriate a using nampesace XXX ?
              My understanding is that the first one is more appropriate for header files (.h) where most of the classes and global object' declarations go; while in source code files (.cpp) the second is more appropriate. Do you agree?

              JKSHJ Online
              JKSHJ Online
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @T3STY said:

              Guys, I was wondering about using this little macro "trick". I have a namespace for most of my code too, but, when should I use a XXX_BEGIN_NAMESPACE ... XXX_END_NAMESPACE, and when is it more appropriate a using nampesace XXX ?
              My understanding is that the first one is more appropriate for header files (.h) where most of the classes and global object' declarations go; while in source code files (.cpp) the second is more appropriate. Do you agree?

              I don't recommend the macro approach; it just makes things messier. Qt uses it to provide flexibility: Users can specify the namespace (OR specify no namespace) to wrap Qt classes, when the Qt libraries are being compiled. Your project is unlikely to need this.

              Be careful if you use using namespace XXX, as it can defeat the purpose of having a namespace: http://julipedia.meroh.net/2013/12/header-files-avoid-c-using-directives.html

              The best approach is to just use prefix your classes/functions/etc with the namespace name.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • T3STYT Offline
                T3STYT Offline
                T3STY
                wrote on last edited by
                #7

                Got it, thank you very much :)
                (nice website you linked, btw)

                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