Results 1 to 6 of 6

Thread: how to declare custom namespace for GUI applications?

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to declare custom namespace for GUI applications?

    Hi,

    I would like to add a custom namespace to my Qt GUI application. How should I do that?

    Here are the details and what I have tried:

    If I create a new Qt console application and I change the main.cpp to this:
    Qt Code:
    1. // ... includes
    2. namespace
    3. {
    4. int x = 1;
    5. }
    6. int main(int argc, char *argv[])
    7. {
    8. // whatever I wish
    9. }
    To copy to clipboard, switch view to plain text mode 
    it works fine.

    But if I create a new Qt GUI project I don't know then where to add the namespace code. If I just try to add it to the mainwindow.h like below then it fails:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. //only this short code is added:
    11. namespace myn
    12. {
    13. int x = 1;
    14. }
    15.  
    16. class MainWindow : public QMainWindow
    17. {
    18. // ... (nothing is changed here compared to the default new project opening)
    19. };
    20.  
    21. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    The error message is that multiple definition for myn::x is given.

    I also tried to add a c++ header as if i wanted to create a new class and include it but it did not work. I tried then to add a class with a proper header and and cpp, and defined the namespace befeore the declaration of the class and the strange result is that it can be compiled and it runs as long as I don't use anything from the namespace. But if I put say a spinbox on the gui and I write into the constructor:
    ui->spinBox->setValue(myn::x);
    it complains again for multiple definition.

    Why is this whole? How can I use than custome namespaces for my Qt Application?

    Szilvi
    Szilvi

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to declare custom namespace for GUI applications?

    This should go into a .cpp file, and in the header file just the extern declaration
    Qt Code:
    1. namespace myn
    2. {
    3. int x = 1;
    4. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to declare custom namespace for GUI applications?

    this may be a silly question but what is an extern declaration? I tried to google it but I just found external variable but that is something else.
    Szilvi

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to declare custom namespace for GUI applications?

    No, that's exactly it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to declare custom namespace for GUI applications?

    aha, so after some tries... this is it:
    I created a new "class" that contains actually no class but looks like this:
    nsclass.h:
    Qt Code:
    1. #ifndef NSCLASS_H
    2. #define NSCLASS_H
    3.  
    4. namespace myn
    5. {
    6. extern int x;
    7. }
    8. #endif // NSCLASS_H
    To copy to clipboard, switch view to plain text mode 
    nsclass.cpp:
    Qt Code:
    1. #include "nsclass.h"
    2.  
    3. namespace myn
    4. {
    5. int x = 11;
    6. }
    To copy to clipboard, switch view to plain text mode 

    and of course I included the header in the mainwindow.h and now it works.


    But I still don't really get the reason why and why not when I leave this whole "extern-game". Could you please explain me this a bit?
    Szilvi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to declare custom namespace for GUI applications?

    Here is a practical explanation.

    Suppose you had the following incl.h file:
    Qt Code:
    1. #ifndef INCL_H
    2. #define INCL_H
    3.  
    4. int x = 10;
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    And then you had a file a.cpp such as this:
    Qt Code:
    1. #include "incl.h"
    2.  
    3. int func1() { return x; }
    To copy to clipboard, switch view to plain text mode 
    and another one, b.cpp, such as this:
    Qt Code:
    1. #include "incl.h"
    2.  
    3. int func2() { return (x+1); }
    To copy to clipboard, switch view to plain text mode 

    Now suppose you have some other piece of code that contains:
    Qt Code:
    1. int x1 = func1();
    2. int x2 = func2();
    To copy to clipboard, switch view to plain text mode 

    When the compiler compiles a.cpp into a.o, it includes the incl.h file which declares a "x" variable and func1() can see the variable and operate on it.
    Now when the compiler compiles b.cpp into b.o, it includes the incl.h file which also declares a "x" variable and func2() can see it and operate on it.
    Now the linker tries to consolidate modules of your program into a single executable. It looks at a.o and sees it has a declaration of variable "x". But b.o also has a declaration of variable "x". To the compiler those are two different variables and it can't cope with such a situation and returns an error. Why? What if it didn't, that's the same variable isn't it? Ok, but what if a.cpp declared x as an integer with value "11" and b.cpp declared it with value "-1"? What would the values of x1 and x2 be after the last snippet?

    What "extern" does is that it says "somewhere out there there is a variable called x". There is no declaration here, just an info for the preprocessor and compiler that "x" is a valid integer variable. It means you have to declare this variable somewhere and that's what you do in your "nsclass.cpp". Then the compiler sees only one "x" variable and two "placeholders" for an x variable. There is no conflict and linking can succeed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following 2 users say thank you to wysota for this useful post:

    gkarthick5 (18th August 2011), szisziszilvi (16th August 2011)

Similar Threads

  1. QTM namespace
    By Aman607 in forum Qt Programming
    Replies: 1
    Last Post: 13th June 2011, 08:00
  2. QTest Namespace
    By chandan in forum Newbie
    Replies: 2
    Last Post: 4th October 2010, 09:45
  3. QTSoapMessage namespace
    By ken123 in forum Qt Programming
    Replies: 0
    Last Post: 26th July 2010, 21:36
  4. using namespace with VS integration
    By jan in forum Qt Tools
    Replies: 0
    Last Post: 29th September 2009, 04:20
  5. namespace problem
    By mhoover in forum Qt Programming
    Replies: 4
    Last Post: 11th July 2006, 22:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.