PDA

View Full Version : Important data available in the entire application | QSharedPointer usage ?



kornicameister
2nd December 2010, 15:24
Hi again
I hit the problem with making one class' object available in the entire application.
The object is created every time I try to establish a new connection with db, and after success it is sent to the core class (core - one which controls main UI). By that I have an access to the connection data which I want to have usable many times in different ways.

So I send a pointer to an object(the object itself is created in the UI which is destroyed right after connection being established) and from that moment I have connection data available from the core-level of my application.

Hope that is explained as simple as possible.
Now, I tried to solve this using QSharedPointer, but I think I did not catch an idea which comes with this class.
But before I ask you to help with making this understandable to me, I would like to ask whether there is claver and simpler solution for that.
The class which contains data looks like below

class ConnectionData : public QObject
{
Q_OBJECT
public:
explicit ConnectionData(QObject *parent = 0);

void set(QString u,QString p,QString h,QString c,short int port);
QString getUser() const;
QString getPass() const;
QString getHost() const;
QString getConnectionName() const;
short int getPort() const;

private:
QString user,
pass,
host,
connectionName;
short int port;
};
so it's not so big, maybe passing by value ?

Timoteo
2nd December 2010, 16:04
You don't need this class. The information that it encapsulates is already available anytime you need it. See QSqlDatabase::database().

kornicameister
2nd December 2010, 20:10
Heh, I did not check where I should have at the first place.
Thanks, that is right that entire information I need are available through QSqlDatabase !

Lykurg
2nd December 2010, 21:16
Slightly OT: But if you need - anywhere else - important data to be available in the entire application see Singleton Pattern.

kornicameister
3rd December 2010, 07:19
the opening description for singleton is not encouraging to use that

It's considered "bad practice"

but thanks anyway and I will read more about and try to understand that

and now the second part of this topic
What's the main difference between QSharedPointer and QPointer in a practical way of using them ?

tbscope
3rd December 2010, 07:38
It's considered bad practice because it is a technique that is not "object oriented".
A better technique is the model/view pattern where the model handles all the data and the view all the displaying.
A singleton is used very often though because of the ease of use.

Basically, QPointer and QSharedPointer are the same, they are guarded pointers. But the behaviour is different in how the references are handled. QSharedPointer counts the references and keeps the pointer alive when there still is a reference. QPointer destroys the pointer when the owner is destroyed while there might be a reference somewhere. Very useful to prevent crashes, however watch out for behavior of the code making use of the reference.

kornicameister
3rd December 2010, 12:26
so if I wanted to pass the pointer between different areas of my program it would be better to use QSharedPointer, as passed from core-level, I am sure that it is still referenced in core-level ? right ?

Timoteo
3rd December 2010, 16:33
so if I wanted to pass the pointer between different areas of my program it would be better to use QSharedPointer, as passed from core-level, I am sure that it is still referenced in core-level ? right ?
Yes, a shared pointer would be appropriate in that case.

Basically, QPointer and QSharedPointer are the same, they are guarded pointers.

No. Firstly, QSharedPointer is not guarded. Secondly, QPointer can only hold QObjects. QSharedPointer does not have this restriction.

tbscope
3rd December 2010, 17:15
I stand corrected.

kornicameister
3rd December 2010, 20:47
and what guarded means in this particular case ?
Question seems to be inappropriate, but my "technical" English is still away from expert-level, so sometimes I can't get something fully, because I simply don't know how to understand some words in related situations :p