Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, July 2007
Re: application wide variables


Message 1 in thread

SI SOL wrote:
> Hello,
>   
Hi.
> I'm new to Qt. I want to store variables in mainwindow that can be reached / accessed from all other dialog window (outside the mainwindow).
If they're truly application-wide variables (i.e. information regarding
the application itself, as opposed to any specific gui component), what
I do is subclass QApplication and add the data to that, providing
accessor methods to my QApplication subclass.  Then that information can
be retrieved from anywhere using the qApp macro.

Don't really know if that's the "best(tm)" way to do things, but it
definitely works, and in my mind it makes sense to keep
application-specific data in the application object.

Hope that helps.

~Darrik

>  Here is my plan :
>
> class MainWindow : public QMainWIndow
> {
> Q_OBJECT
> public:
>    int some_public_function();
>    some_public_procedure();
>    some_public_variables;
> protected:
>    ...
>    ...
> private slots:
>    ...
>    ...
> private:
>    ...
>    ...
> }
>
> Is this the correct / recomended way to do such thing in Qt ?. What is the best practice to do this thing in Qt  ?.  
>
> Thanks in advance.
> regard,
> si sol
>
>
>
>
>
>
>
>       ____________________________________________________________________________________
> Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.
> http://autos.yahoo.com/green_center/ 
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>   


-- 
 [ signature omitted ] 

Message 2 in thread

On Sunday 01 July 2007, darrik mazey wrote:
> SI SOL wrote:
> > Hello,
>
> Hi.
>
> > I'm new to Qt. I want to store variables in mainwindow that can be
> > reached / accessed from all other dialog window (outside the mainwindow).
>
> If they're truly application-wide variables (i.e. information regarding
> the application itself, as opposed to any specific gui component), what
> I do is subclass QApplication and add the data to that, providing
> accessor methods to my QApplication subclass.  Then that information can
> be retrieved from anywhere using the qApp macro.
>
> Don't really know if that's the "best(tm)" way to do things, but it
> definitely works, and in my mind it makes sense to keep
> application-specific data in the application object.

Hi,

the classical way to provide a single instance of a class which is accessible 
application wide is the so called singleton pattern.. 

[disclaimer: i didn't check if this compiles]

// header

class GlobalStuff
{
	static GlobalStuff *_instance;


	public:
		static GlobalStuff *get_instance ();

		// some public member vars..
		int _foo;
		std::string _bar;
		SomeClass _some_member;

		~GlobalStuff()
	protected:
		GlobalStuff();
};

// implementation

GlobalStuff *GlobalStuff::_instance = 0;

GlobalStuff::~GlobalStuff ()
{
	_instance = 0;
}

GlobalStuff::GlobalStuff ()
{

}

GlobalStuff *GlobalStuff::get_instance ()
{
	if (_instance == 0)
	{
		_instance = new GlobalStuff();
		return _instance;
	}
	else
	{
		return _instance;
	}
}

You can get a pointer to the single instance from everywhere in the app by 
calling the static get_instance() method. If the single instance doesn't 
exist at that point in time yet, it will be created automatically.

Regards,
Flo

P.S.: I guess, the QApplication class uses a similar mechanism.

--
 [ signature omitted ] 

Message 3 in thread

From: darrik mazey <darrik@xxxxxxxxxxxxxxxxxxx>
To: qt-interest@xxxxxxxxxxxxx
Sent: Monday, July 2, 2007 1:39:18 AM
Subject: Re: application wide variables

>If they're truly application-wide variables (i.e. information regarding
>the application itself, as opposed to any specific gui component), what
>I do is subclass QApplication and add the data to that, providing
>accessor methods to my QApplication subclass.  

Yes , it's truly application-wide variables and not specific GUI component.

>Then that information can be retrieved from anywhere using the qApp macro.

Just to speed up my learning process, could you give me simple sample code ?.

Anyway, thanks a lot for your reply.
Regards,

si sol







      ____________________________________________________________________________________
Luggage? GPS? Comic books? 
Check out fitting gifts for grads at Yahoo! Search
http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz

--
 [ signature omitted ] 

Message 4 in thread

Here's a small example.

Hope this helps a little.

~Darrik

SI SOL wrote:
> From: darrik mazey <darrik@xxxxxxxxxxxxxxxxxxx>
> To: qt-interest@xxxxxxxxxxxxx
> Sent: Monday, July 2, 2007 1:39:18 AM
> Subject: Re: application wide variables
>
>   
>> If they're truly application-wide variables (i.e. information regarding
>> the application itself, as opposed to any specific gui component), what
>> I do is subclass QApplication and add the data to that, providing
>> accessor methods to my QApplication subclass.  
>>     
>
> Yes , it's truly application-wide variables and not specific GUI component.
>
>   
>> Then that information can be retrieved from anywhere using the qApp macro.
>>     
>
> Just to speed up my learning process, could you give me simple sample code ?.
>
> Anyway, thanks a lot for your reply.
> Regards,
>
> si sol
>
>
>
>
>
>
>
>       ____________________________________________________________________________________
> Luggage? GPS? Comic books? 
> Check out fitting gifts for grads at Yahoo! Search
> http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>   


-- 
 [ signature omitted ] 
// // // application.h // // //

#ifndef ___APPLICATION_H
#define ___APPLICATION_H

#include <QApplication>

// so that you get an Application* instead of a QApplication*
/*
 * you can access your application from anywhere (that includes application.h)
 * with cApp->method()
 */
#define cApp ((Application *) qApp)

class Application : public QApplication
{
	public:
		Application(int &argc, char **argv);
		~Application();

		// you can store objects in the Application object
		MyApplicationDataObject *getData();
		// or just data with a get method
		int getIntData();
		// and set method
		void setIntData(const int d);

	private:
		MyApplicationDataObject *m_dataObject;
		int m_intData;
};

#endif // ___APPLICATION_H

// // // application.cpp // // // 

#include "application.h"

Application::Application(int &argc, char **argv) :
	QApplication(argc, argv)
{
	m_dataObject = new MyApplicationDataObject();
	m_intData = 0;
}

Application::~Application()
{
	if (m_dataObject) {
		delete(m_dataObject);
		m_dataObject = 0;
	}
}

MyApplicationDataObject *Application::getData()
{
	return(m_dataObject);
}

int Application::getIntData()
{
	return(m_intData);
}

void Application::setIntData(const int d)
{
	m_intData = d;
}



Message 5 in thread

Thanks a lot for your sample code. 

>From: darrik mazey <darrik@xxxxxxxxxxxxxxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Sent: Tuesday, July 3, 2007 11:54:37 AM
>Subject: Re: application wide variables

>Here's a small example.

>Hope this helps a little.

>~Darrik







       
____________________________________________________________________________________
Got a little couch potato? 
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz 

--
 [ signature omitted ] 

Message 6 in thread

>----- Original Message ----
>From: Florian Schmidt <mista.tapas@xxxxxxx>
>To: qt-interest@xxxxxxxxxxxxx
>Cc: darrik mazey <darrik@xxxxxxxxxxxxxxxxxxx>
>Sent: Sunday, July 15, 2007 10:16:27 PM
>Subject: Re: application wide variables
>
>On Sunday 01 July 2007, darrik mazey wrote:
>> SI SOL wrote:
>> > Hello,
>>
>> Hi.
>>
>> > I'm new to Qt. I want to store variables in mainwindow that can be
>> > reached / accessed from all other dialog window (outside the mainwindow).
>>
>> If they're truly application-wide variables (i.e. information regarding
>> the application itself, as opposed to any specific gui component), what
>> I do is subclass QApplication and add the data to that, providing
>> accessor methods to my QApplication subclass.  Then that information can
>> be retrieved from anywhere using the qApp macro.
>>
>> Don't really know if that's the "best(tm)" way to do things, but it
>> definitely works, and in my mind it makes sense to keep
>> application-specific data in the application object.
>
>Hi,
>
>the classical way to provide a single instance of a class which is accessible 
>application wide is the so called singleton pattern.. 
>
>[disclaimer: i didn't check if this compiles]
>
>// header
>
>class GlobalStuff
>{
>    static GlobalStuff *_instance;
>
>
>    public:
>        static GlobalStuff *get_instance ();
>
>        // some public member vars..
>        int _foo;
>        std::string _bar;
>        SomeClass _some_member;
>
>        ~GlobalStuff()
>    protected:
>        GlobalStuff();
>};
>
>// implementation
>
>GlobalStuff *GlobalStuff::_instance = 0;
>
>GlobalStuff::~GlobalStuff ()
>{
>    _instance = 0;
>}
>
>GlobalStuff::GlobalStuff ()
>{
>
>}
>
>GlobalStuff *GlobalStuff::get_instance ()
>{
>    if (_instance == 0)
>    {
>        _instance = new GlobalStuff();
>        return _instance;
>    }
>    else
>    {
>        return _instance;
>    }
>}
>
>You can get a pointer to the single instance from everywhere in the app by 
>calling the static get_instance() method. If the single instance doesn't 
>exist at that point in time yet, it will be created automatically.
>
>Regards,
>Flo
>
>P.S.: I guess, the QApplication class uses a similar mechanism.

Thanks for your reply. Using QApplication (suggestion from previous thread), I've managed to build one simple application with several
aplication-wide variables / objects. I haven't check this design for
threading issue though, but I think it satisfy my (current) need. Also,
thanks for all who have replied to this thread.







       
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469

--
 [ signature omitted ] 

Message 7 in thread

SI SOL wrote:
> 
> Thanks for your reply. Using QApplication (suggestion from previous thread), I've managed to build one simple application with several
> aplication-wide variables / objects. I haven't check this design for
> threading issue though, but I think it satisfy my (current) need. Also,
> thanks for all who have replied to this thread.

I use this same design for threading, but instead of putting global
application data directly in my subclass of QApplication, I place it in
an object with a QMutex, then place that object in QApplication.  This
way every thread has access to the mutex to lock before accessing the
data directly.  So basically all access to the global data happens thusly:

ThreadSafeData *ts = sApp->threadSafeData();

ts->lockMutex();

int d = ts->readSomeData();
ts->changeSomeData(new_data);

ts->unlockMutex();

(where sApp is a wrapper on the qApp macro that casts the QApplication
pointer to my subclass).

Hope that helps.  If you want a more extensive example, feel free to
post to the list or email me directly.

Cheers,
Darrik

--
 [ signature omitted ]