PDA

View Full Version : ohhh my, Getting handle to main app



sticcino
18th June 2008, 16:35
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

marcel
18th June 2008, 17:07
See qApp (http://doc.trolltech.com/4.4/qapplication.html#qApp) or QApplication::instance() (http://doc.trolltech.com/latest/qapplication.html#instance).
You'll need to cast that to your specific QApplication subclass.

sticcino
18th June 2008, 18:11
Thank you very much,
Johnny

sticcino
18th June 2008, 22:31
Hi,

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

http://www.qtcentre.org/forum/f-qt-programming-2/t-ohhh-my-getting-handle-to-main-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. :crying:

Thanks again,
Johnny

mcosta
18th June 2008, 23:09
Create your Application Class end redefine the "qApp" macro.



// MyApp.h

class MyApp;

#if defined(qApp)
#undef qApp
#endif
#define qApp (static_cast<MyApp*>(QCoreApplication::instance()))

class MyApp: public QApplication
{
...
};


The example code is derived from qapplication.h

sticcino
19th June 2008, 21:17
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...:


#include myapp.h
...

class MyApp;
...

protected:
MyApp * m_pApp;



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

pherthyl
20th June 2008, 21:02
Thank you for your help.


#include myapp.h
...

class MyApp;
...

protected:
MyApp * m_pApp;



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.