PDA

View Full Version : Qt and global variables



Morea
23rd January 2007, 18:48
Hi.
I was wondering how can I use global variables in my Qt program? Where should I create the global Qt objects, in the main.cpp file (that is a very short file), or where?

The other option is to create a global object and store a pointer to that object in everyother object that I then create (and that might be A LOT).

Any suggestions?

gri
23rd January 2007, 20:17
I think that depends on the design of your program. I would never use global variables. They are not thread safe. If you need a single instantiating object do something like this (which is also not thread safe):

class MyClass
{
MyClass() {};
virtual ~MyClass() {};

public:
static MyClass* instance()
{
if ( !m_pInstance )
m_pInstance = new MyClass;

return m_pInstance;
}

void doSomething()
{
// ...
}

protected:
static MyClass* m_pInstance;
};

MyClass* MyClass::m_pInstance = NULL;

Now you could use "MyClass* pointer = MyClass::instance(); pointer->doSomething();".
In that way you can also decide in which thread the instance will be created and no object exists before QApplication was instantiated. If you derive from QObject or not is your choice but don't forget to delete :)

kandalf
24th January 2007, 04:27
Hi.
I was wondering how can I use global variables in my Qt program?
Why would you do that?


Any suggestions?
Yes, don't use them. Use the Singletton Pattern described by gri.


Cheers.

tiva
24th January 2007, 08:00
Request you not to use global variables.
Try to stick on to true OOP concepts.
Qt is a wonderful framework which smoothly enables a
developer to create applications in true object domain.

I guess you are not trying to mix 'C' kind of coding using C++ compiler !

Morea
24th January 2007, 15:11
The singleton code looks nice. I'll try to use that. Thanks!

Methedrine
24th January 2007, 16:38
The singleton code looks nice. I'll try to use that. Thanks!

The Singleton implementation described is used widely but also contains a race-condition when it comes to use in a multithreaded program (see Alexandrescu's "Modern C++ Design" for further details).



class MyClass
{
MyClass() {};
virtual ~MyClass() {};

public:
static MyClass* instance()
{
static QMutex mutex;
if ( !m_pInstance )
{
mutex.lock();
if (!m_pInstance)
m_pInstance = new MyClass;
mutex.unlock();
}

return m_pInstance;
}

void doSomething()
{
// ...
}

protected:
static MyClass* m_pInstance;
};

MyClass* MyClass::m_pInstance = NULL;

stevey
25th January 2007, 07:55
Aye on the last post.

Singleton's are a great concept but just be aware that you have to pay careful attention to making them thread safe.

jpn
25th January 2007, 09:48
Methedrine, would you mind putting the example on our wiki? The singleton pattern has been suggested/discussed several times on the forums. One could just refer to the wiki article in the future.. ;)

Methedrine
25th January 2007, 12:04
Wiki-Entry Done. (http://wiki.qtcentre.org/index.php?title=Singleton_Pattern) :cool:

Lykurg
1st February 2007, 20:09
Sorry,

maybe I am too stupid, but your code doesn't work for me. I take your code and just changed the name of the class:

#ifndef CONFIGURATION_H
#define CONFIGURATION_H

#include <QtCore>
#include <QtSql>

class configuration
{
public:
static configuration* instance()
{
static QMutex mutex;
if ( !m_instance )
{
mutex.lock();
if ( !m_instance )
m_instance = new configuration;
mutex.unlock();
}
return m_instance;
}

private:
configuration(){};
configuration( const configuration& _instance ){};
static configuration* m_instance;

// [...] some other functions

};

configuration* configuration::m_instance = NULL;

#endif

When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
.comp/sw_normal.o: In function `operator delete(void*, void*)':
/usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
.comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
collect2: ld returned 1 exit status


And by the way must it in your example in the wiki not be

Singleton* Singleton::m_Instance = 0;
instead of

Singleton* m_Instance = 0;



Thanks,
Lykurg

P.s: if I comment the function (on bottom of the file) in qvortaro.cpp:194 out, then this error pointed to now last function on the file.

jacek
1st February 2007, 21:06
When compiling I receive the error (which I don't understand == don't know what to do/where the error could be):
.comp/sw_normal.o: In function `operator delete(void*, void*)':
/usr/include/qt4/QtCore/qatomic_i386.h:62: multiple definition of `configuration::m_instance'
.comp/qvortaro.o:/home/lykurg/Programmierung/qvortaro/src/qvortaro.cpp:194: first defined here
collect2: ld returned 1 exit status
You have placed the definition of static member variable in a header file, therefore every .cpp file that includes it has it's own copy of that variable. Better move that line to one of the .cpp files, say configuration.cpp.

Methedrine
2nd February 2007, 00:42
And by the way must it in your example in the wiki not be

Singleton* Singleton::m_Instance = 0;
instead of

Singleton* m_Instance = 0;


Yes, of course. Thank you, Lykurg for spotting it, and thank you, jacek, for correcting it before I spotted this thread :)