Results 1 to 11 of 11

Thread: How to use an external variable ?

  1. #1
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use an external variable ?

    Hi,

    I want to make a couple of generic data fields program-wide available. I tried to pack them all into just a few global variables, but I don't see how I can get rid of a global variable altogether. Is there a better way ?
    Or rather, is there a quick better way ? I just need to get this program functional for now.
    Currently I'm using global extern variables like this:

    data.h
    Qt Code:
    1. #ifndef DATA_H
    2. #define DATA_H
    3.  
    4. #include <QDebug>
    5.  
    6. class Dummy
    7. {
    8. public:
    9. Dummy()
    10. {
    11. qDebug() << "Dummy constructor";
    12. }
    13.  
    14. void show()
    15. {
    16. qDebug() << "Dummy show";
    17. }
    18. };
    19.  
    20. extern Dummy dummy;
    21.  
    22. #endif // DATA_H
    To copy to clipboard, switch view to plain text mode 

    data.cpp
    Qt Code:
    1. #include "data.h"
    2.  
    3. Dummy dummy; // alternate defintion 1
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "data.h"
    2.  
    3. Dummy dummy; // alternate definition 2
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. dummy.show();
    8.  
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that I wonder if the 'dummy' variable is actually ever initialized. The compiler doesn't complain, but neither of the qDebug() outputs is ever shown.
    I tried putting the definition of the dummy variable in the data.cpp and the main.cpp files, respectively. But no effect.

    However, if I put illegal code in the contrustor, the program does crash. (Though only if I put the definition of 'dummy' in 'data.cpp', not if it's in 'main.cpp'.)
    So that suggests that the constructor is in fact called.

    I may be just missing something very simple. But I can't conceive of it right now.

    Thanks for any help.
    Last edited by Computer Hater; 22nd June 2011 at 23:03.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    You may wish to research singletons.

  3. #3
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    Hmm. Maybe it is just that everything works, but qDebug() doesn't. That would be consistent with the observation.
    But why would that be ?

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

    Default Re: How to use an external variable ?

    both "alternate defintion 1" and "alternate defintion 2" are ok, either one of these will work, you should be getting the qDebug() messages, Under Windows, the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger. (check "Application Ouput" tab is using Qt Creator)

  5. #5
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    Quote Originally Posted by Santosh Reddy View Post
    both "alternate defintion 1" and "alternate defintion 2" are ok, either one of these will work, you should be getting the qDebug() messages, Under Windows, the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger. (check "Application Ouput" tab is using Qt Creator)
    The qDebug() output from the Dummy class functions is not showing in Application Output.
    But when I use qDebug() from my main function, e.g. if I add a public QString to the Dummy class, set its value in the Dummy constructor and access and output it with qDebug() from 'main', then it works.
    I forgot to mention that I had done this too.

    Quote Originally Posted by squidge View Post
    You may wish to research singletons.
    Well, I stumbled on this thread when posting this.
    The singleton code looks a bit cumbersome to me. Does it have any intrinsic advantages or is it just a work-around to avoid global variables ? Are global variables just poorly implemented in C++ ?
    I'd just like to understand why it's good to use singletons.

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

    Default Re: How to use an external variable ?

    does your .pro file anything more than this?
    Qt Code:
    1. HEADERS += \
    2. data.h
    3.  
    4. SOURCES += \
    5. data.cpp \
    6. main.cpp
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    My project file looks like this:

    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2010-11-25T17:13:18
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. TARGET = Test
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. mainwindow.cpp \
    15. data.cpp \
    16. ssvector.cpp
    17.  
    18. HEADERS += mainwindow.h \
    19. data.h \
    20. ssvector.h
    21.  
    22. FORMS += mainwindow.ui
    To copy to clipboard, switch view to plain text mode 

    For the current test I've outcommented all calls to mainwindow and ssvector (in the source code), as they were from earlier tests and were only still in there for later use.

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

    Default Re: How to use an external variable ?

    test with the .pro file posted my me...

  9. #9
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    qDebug() still doesn't work. Also, now the qDebug() in the main function doesn't work anymore.
    Although that may be because at first I manipulated the .pro file myself.
    When I did, Creator crashed, then I modified it again and it didn't crash anymore, but the main function qDebug() output was gone.
    After that I copy-pasted your code and it's the same behaviour.


    Added after 4 minutes:


    I mean main function output like this:

    qDebug() << dummy.str;

    where 'str' is a public QString in Dummy initialized in the constructor. This used to show up correctly.
    Last edited by Computer Hater; 23rd June 2011 at 00:27.

  10. #10
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    About the problem that I cannot even use qDebug() in the 'main' function to output a public member QString from the Dummy class (or any other text for that matter): I cannot reliably reproduce this problem. It's there one moment, then I change something, undo the change and it suddenly works. It is apparently totally random. I have no clue.

    But the problem that the qDebug() output from the Dummy class functions doesn't work is persistent.
    I hope that if this problem is solved, the inconsistent behaviour of qDebug() in the 'main' function will disappear as well.

  11. #11
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use an external variable ?

    Update/Conclusion:

    I haven't figured out why qDebug() doesn't work for regular program runs anymore. The problem seems to be limited though to stuff that is initialized before the main function (external and static initializations), so maybe there's a reason for that.
    Also, if I use the actual debugging option in Qt Creator, all of the qDebug() output shows up regularly.
    So I can work with that.

Similar Threads

  1. variable
    By Atuti2009 in forum Qt Programming
    Replies: 8
    Last Post: 18th November 2009, 09:04
  2. Signal from variable
    By waynew in forum Newbie
    Replies: 2
    Last Post: 18th November 2009, 01:18
  3. QT and variable scope
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 21:32
  4. Getting Type of an variable
    By hgedek in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2007, 15:37
  5. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.