Results 1 to 10 of 10

Thread: Important data available in the entire application | QSharedPointer usage ?

  1. #1
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Important data available in the entire application | QSharedPointer usage ?

    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
    Qt Code:
    1. class ConnectionData : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit ConnectionData(QObject *parent = 0);
    6.  
    7. void set(QString u,QString p,QString h,QString c,short int port);
    8. QString getUser() const;
    9. QString getPass() const;
    10. QString getHost() const;
    11. QString getConnectionName() const;
    12. short int getPort() const;
    13.  
    14. private:
    15. QString user,
    16. pass,
    17. host,
    18. connectionName;
    19. short int port;
    20. };
    To copy to clipboard, switch view to plain text mode 
    so it's not so big, maybe passing by value ?
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    You don't need this class. The information that it encapsulates is already available anytime you need it. See QSqlDatabase::database().

  3. The following user says thank you to Timoteo for this useful post:

    kornicameister (2nd December 2010)

  4. #3
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    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 !
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    Slightly OT: But if you need - anywhere else - important data to be available in the entire application see [WIKI]Singleton Pattern[/WIKI].

  6. #5
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    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 ?
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  7. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    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.
    Last edited by tbscope; 3rd December 2010 at 07:43.

  8. #7
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    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 ?
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  9. #8
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    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.

  10. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    I stand corrected.

  11. #10
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Important data available in the entire application | QSharedPointer usage ?

    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
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

Similar Threads

  1. Why is my application's CPU usage always > 50%
    By richardander in forum Qt Programming
    Replies: 10
    Last Post: 14th October 2010, 22:22
  2. How catch key press and key release for entire application
    By hubbobubbo in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 20:53
  3. QMessageBox closes entire application
    By Leoha_Zveri in forum Qt Programming
    Replies: 2
    Last Post: 19th September 2009, 11:44
  4. Replies: 6
    Last Post: 5th August 2009, 10:40
  5. Qprocess...hanging entire application!!!!
    By nupul in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2006, 14:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.