Results 1 to 7 of 7

Thread: ohhh my, Getting handle to main app

  1. #1
    Join Date
    Jun 2008
    Location
    Canada
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Red face ohhh my, Getting handle to main app

    Hi,

    I'm new to the x-programming world of QT and lov it!!.

    I have a hopefully simple problem, I need to get access to the main apps instance so I can utilize some of the functions and variables required throught my app.

    So in my main app: MyApp.h, i have a database handle [QSqlDatabase m_pPrimarydb] that i declared as public.
    public:
    QSqlDatabase m_pPrimarydb;

    In my MSVC apps i would have a app pointer declared as "theApp" and would be able to use it throught the app's various classes as:

    if(theApp->m_pPrimarydb) {
    ...
    }

    How the heck can I do this in QT ?, i searched the examples but cannot find anything that can assist me.

    Thanks,
    Johnny

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ohhh my, Getting handle to main app

    See qApp or QApplication::instance().
    You'll need to cast that to your specific QApplication subclass.

  3. #3
    Join Date
    Jun 2008
    Location
    Canada
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: ohhh my, Getting handle to main app

    Thank you very much,
    Johnny

  4. #4
    Join Date
    Jun 2008
    Location
    Canada
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question how to cast qapp to subclass

    Hi,

    I was kindly told what needed to be done to implement this:

    http://www.qtcentre.org/forum/f-qt-p...app-14270.html


    can anyone give me a sample of how to cast the qapp instance so i can use it in other areas/classes in my app. There are a few global functions and variables that i need to access from other classes.

    Thanks again,
    Johnny
    Last edited by sticcino; 18th June 2008 at 22:56. Reason: spelling errors

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to cast qapp to subclass

    Create your Application Class end redefine the "qApp" macro.

    Qt Code:
    1. // MyApp.h
    2.  
    3. class MyApp;
    4.  
    5. #if defined(qApp)
    6. #undef qApp
    7. #endif
    8. #define qApp (static_cast<MyApp*>(QCoreApplication::instance()))
    9.  
    10. class MyApp: public QApplication
    11. {
    12. ...
    13. };
    To copy to clipboard, switch view to plain text mode 

    The example code is derived from qapplication.h
    A camel can go 14 days without drink,
    I can't!!!

  6. #6
    Join Date
    Jun 2008
    Location
    Canada
    Posts
    10
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Re: ohhh my, Getting handle to main app

    Thank you for your help.

    I don't think i'm understanding this casting with QT.
    so i changed my manin class as suggested in myapp.h, no problems

    in the class i need to access myapp class members, in its header file...:
    Qt Code:
    1. #include myapp.h
    2. ...
    3.  
    4. class MyApp;
    5. ...
    6.  
    7. protected:
    8. MyApp * m_pApp;
    To copy to clipboard, switch view to plain text mode 


    when watching m_pApp, i can see all the members but they are not initialized/accessible/

    m_pApp 0xbaadf00d {m_pPrimarydb={...} view=??? m_pWaveSummary=??? ...} MixxxITApp *

    + m_pPrimarydb {d=??? } QSqlDatabase


    i cannot see the defined qApp that was setup in the .h file also.

    thanks,
    johnny
    Last edited by sticcino; 19th June 2008 at 21:18. Reason: added addtional text

  7. #7
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ohhh my, Getting handle to main app

    Quote Originally Posted by sticcino View Post
    Thank you for your help.
    Qt Code:
    1. #include myapp.h
    2. ...
    3.  
    4. class MyApp;
    5. ...
    6.  
    7. protected:
    8. MyApp * m_pApp;
    To copy to clipboard, switch view to plain text mode 


    when watching m_pApp, i can see all the members but they are not initialized/accessible/
    Well, have you initialized it?

    i cannot see the defined qApp that was setup in the .h file also.

    thanks,
    johnny
    You mean it's not defined? using qApp should work, since you have it defined and are including your qapplicaiton header. You don't need a global variable m_pApp.. Just use qApp.

Similar Threads

  1. OS/X how to get physical display handle
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2009, 19:51
  2. Replies: 4
    Last Post: 10th March 2007, 18: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.