Results 1 to 11 of 11

Thread: main.cpp variable access question

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default main.cpp variable access question

    Hi to all!

    Can somene please tell me how to access variable appSettings, declared in main.cpp:
    Qt Code:
    1. // qt includes
    2. #include <QtGui>
    3. #include <QApplication>
    4. #include <QPlastiqueStyle>
    5.  
    6. // custom includes
    7. #include "cmainwindow.h"
    8. #include "globals.h"
    9. #include "CApplicationSettings.h"
    10.  
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. ....
    15. CApplicationSettings appSetings(strIniFileName,
    16. QSettings::IniFormat,
    17. qApp); // sets up app settings
    18.  
    19. ....
    20. }
    To copy to clipboard, switch view to plain text mode 
    from class, that is defined through
    Qt Code:
    1. #include "cmainwindow.h"
    To copy to clipboard, switch view to plain text mode 
    in main.cpp? Is this possible?
    Qt 5.3 Opensource & Creator 3.1.2

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

    Default Re: main.cpp variable access question

    No, this variable is only visible from inside main(). If you want to access it from outside, you need to provide a pointer to it either through a global variable or through some method of a class that itself has access to that variable or a static method returning the instance (just like qApp and QCoreApplication::instance() are used).

  3. #3
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: main.cpp variable access question

    hmm, then I must rewrite CApplicationSettings like I did with CDatabaseSettings: make it singleton?

    Or, where should I put pointer to this variable?
    Last edited by MarkoSan; 10th March 2008 at 03:31. Reason: updated contents
    Qt 5.3 Opensource & Creator 3.1.2

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

    Default Re: main.cpp variable access question

    You can use a global variable.
    Qt Code:
    1. CApplicationSettings *settings;
    2.  
    3. int main(...){
    4. CApplicationSettings Settings;
    5. settings = &Settings;
    6. }
    7.  
    8. extern CApplicationSettings *settings;
    9. void xxx(){ settings->doSomething(); }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to wysota for this useful post:

    MarkoSan (10th March 2008)

  6. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Unhappy Re: main.cpp variable access question

    But wysotta, why do I need both declarations from line 1 AND from line 8? Otherwise, I rewritten the code according to your hint, it compiles without errors. Now let me check this pointer. Thanks man!

    Well, when i try to acces global variable from other class, I get this error:
    Qt Code:
    1. mingw32-make
    2. mingw32-make -f Makefile.Debug
    3. mingw32-make[1]: Entering directory `C:/Documents and Settings/markofr/workspace/SettingsEditor'
    4. c:\Qt\4.3.4\bin\uic.exe cmainwindow.ui -o ui_cmainwindow.h
    5. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CDataInputWidget.o CDataInputWidget.cpp
    6. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CClientVisualSettingsPage.o CClientVisualSettingsPage.cpp
    7. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CLanguageSettingsPage.o CLanguageSettingsPage.cpp
    8. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CDatabaseSettingsPage.o CDatabaseSettingsPage.cpp
    9. g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtCore" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include\QtGui" -I"..\..\..\..\Qt\4.3.4\include" -I"c:\Qt\4.3.4\include\ActiveQt" -I"debug" -I"." -I"..\..\..\..\Qt\4.3.4\mkspecs\win32-g++" -o debug\CSettingsWindow.o CSettingsWindow.cpp
    10. CSettingsWindow.cpp: In member function `void CSettingsWindow::saveSettings()':
    11. CSettingsWindow.cpp:124: error: `pAppSettings' undeclared (first use this function)
    12. CSettingsWindow.cpp:124: error: (Each undeclared identifier is reported only once for each function it appears in.)
    13. mingw32-make[1]: *** [debug/CSettingsWindow.o] Error 1
    14. mingw32-make: *** [debug] Error 2
    15. mingw32-make[1]: Leaving directory `C:/Documents and Settings/markofr/workspace/SettingsEditor'
    To copy to clipboard, switch view to plain text mode 
    Last edited by MarkoSan; 10th March 2008 at 16:36. Reason: updated contents
    Qt 5.3 Opensource & Creator 3.1.2

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

    Default Re: main.cpp variable access question

    Line 8 has to be in a file where you want to use the variable declared in the file containing line 1. Otherwise you'll get wat you got in line 11 of your compiler output.

  8. The following user says thank you to wysota for this useful post:

    MarkoSan (10th March 2008)

  9. #7
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: main.cpp variable access question

    Just one more thing I want to ask about "background" of this code implementation. How does compiler knows the pointer from main and extern pointer are the same? Because of same name? And, I've read today about this topic we are discussing and it seems these global pointers are very dangerous in program, because it's value can be changed without notifiacation, which leads to possible application crash. How do I make this pointer const?
    Qt 5.3 Opensource & Creator 3.1.2

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

    Default Re: main.cpp variable access question

    Quote Originally Posted by MarkoSan View Post
    How does compiler knows the pointer from main and extern pointer are the same? Because of same name?
    Yes, the name and the extern keyword.

    And, I've read today about this topic we are discussing and it seems these global pointers are very dangerous in program, because it's value can be changed without notifiacation, which leads to possible application crash.
    Correct. But if you're the only programmer and you know what you are doing, you can safely use it.

    How do I make this pointer const?
    You can't. As an alternative you can implement the singleton pattern for the class.

  11. The following user says thank you to wysota for this useful post:

    MarkoSan (10th March 2008)

  12. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: main.cpp variable access question

    Wysota, thank you very very much for clarification of this topic. Just one final question: What are other design patterns beside singleton design pattern for solving this problem? So I can google it up and read about it ...
    Qt 5.3 Opensource & Creator 3.1.2

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

    Default Re: main.cpp variable access question

    I don't think there are other patterns that might be helpful, although I'm not much of an expert on design patterns.

  14. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: main.cpp variable access question

    Ok, will google it anyway.
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 17:49
  2. Variable question
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 15th March 2007, 14:59
  3. a question conerning code in main.cpp
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2006, 16:55
  4. Simple Question on Variable initialization
    By Naveen in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2006, 11:01

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.