PDA

View Full Version : Like a session in PHP



wirasto
17th November 2009, 09:46
I'm so impressed with QSqlDatabase. If I set like this..



QSqlDatabase db=QSqlDatabase::addDatabase(...);
db.setHostName("abc");


Then, I can get value "abc" in everywhere with



qDebug() << QSqlDatabase::database().hostName()


I think that is like set $_SESSION["hostName"]="abc" in PHP.
How to create like that with Qt ?

Lykurg
17th November 2009, 13:20
Please, do not post multiple the same issue, and this one I haven't understand... Just because of your former post...

Look for a Singleton pattern (http://wiki.qtcentre.org/index.php?title=Singleton_pattern).

wirasto
17th November 2009, 15:19
Okh :(

I can get my variable value in every where with that ?

I want have a function like $_SESSION[..] in PHP. So if I need the variable value in other dialog, i not need pass the variable to other dialog any more.

Sorry, with my english..

Lykurg
17th November 2009, 15:26
I can get my variable value in every where with that ?
Have you take a look at it? Yes you can, that's the reason I post it;)


int abc = Singleton::instance()->getMyFancyVariable();

wysota
17th November 2009, 16:55
Just to make it clear - $_SESSION is not global anymore in php so you can't access it from anywhere in your script unless you register it as global. And if you do, it's direct equivalent in C++ is a global variable as well.


QHash<QString,QVariant> _SESSION; // in cpp somewhere
//...
extern QHash<QString,QVariant> _SESSION; // in .h
// ...

_SESSION["x"] = "y";

Of course it's much better to use a singleton as suggested, both in C++ and PHP.