Results 1 to 12 of 12

Thread: QSharedDate - possible misunderstanding

  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 QSharedDate - possible misunderstanding

    I need to solve the problem with sharing some data between my classes
    I thought it would be good idea to check out how QSharedData works, so I cut out the data which used to be encapsulated and shared via static object and made my class public of QSharedData

    but I think I misunderstood something because real sharing is not what I received...
    what I need to have are classes with mutual access to the list of columns, list of tables (both lists contains cols,tabs from db as strings) which are not modified and there are three variables which can be modified in runtime...

    I read the documentation and I think that it is achievable, therefore there must be something I've done wrong

    class with data looks like below

    Qt Code:
    1. class QueryHandlerData : public QSharedData{
    2. public:
    3. QueryHandlerData(){
    4. codec = QTextCodec::codecForName("Windows-1250");
    5. //appending columns and tables here
    6. }
    7. QString connectionName;
    8. QString cStatus;
    9. int lastInsertedId; //can be redundant,QSqlDatabase has the feature which allows to obtain it anywhere
    10. QStringList columns;
    11. QStringList tables;
    12. QTextCodec *codec;
    13. };
    To copy to clipboard, switch view to plain text mode 

    but I saw on forum, that it should implement detach() method, is it a problem here ?
    Another problem can be that I can not fully understand the meaning of Implicit and Explicit sharing
    Does the implicit means that shared data is like-hidden, because I that's what I found in the dictionary
    Last edited by kornicameister; 2nd February 2011 at 16:53.
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDate - possible misunderstanding

    but I saw on forum, that it should implement detach() method, is it a problem here ?
    Another problem can be that I can not fully understand the meaning of Implicit and Explicit sharing
    Does the implicit means that shared data is like-hidden, because I that's what I found in the dictionary
    Did you read this part of the documentation?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #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: QSharedDate - possible misunderstanding

    Yes, I did.
    So I need to implement detach method and use it every time I want to modify shared data ?

    But still. will I be able to benefit from true sharing od data ?
    For example

    I modify variable A in point X of the program in the object 1
    And than I need to access A from the object 2 in the point Y of the runtime.
    Will I get what was set in the point X ?
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDate - possible misunderstanding

    I modify variable A in point X of the program in the object 1
    And than I need to access A from the object 2 in the point Y of the runtime.
    Will I get what was set in the point X ?
    Yes.

    But still. will I be able to benefit from true sharing od data ?
    Well again, the benefit of implicit sharing is:
    The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.
    So everywhere where you need to access the data but not change it, implicit sharing is a benefit.
    Last edited by high_flyer; 4th February 2011 at 20:49.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #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: QSharedDate - possible misunderstanding

    but how implicit sharing differs from static object in which my data is encapsulated ?
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDate - possible misunderstanding

    because a static object is only one object.
    If you copy it, you will have to create another full copy of that object.
    You can have any number of implicit shared objects, and if you change the data in one of them, only that object will be changed (copy on demand).
    So implicit sharing allows you to transparently use many "copies" that are really only one, so long they "are the same", but as soon as one is changed, it is not the same as the others any more, and that is done automatically.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #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: QSharedDate - possible misunderstanding

    you said copying ?
    but does not Q_DISABLE_COPY macro prevent from copying ?
    all the data I need to have are the data used in classes derived from QObject
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSharedDate - possible misunderstanding

    you said copying ?
    Yes. As in "by value".
    but does not Q_DISABLE_COPY macro prevent from copying ?
    No.
    This macro is used for classes that want to forbid the use of a copy constructor:
    http://doc.trolltech.com/4.7/qobject...Q_DISABLE_COPY

    all the data I need to have are the data used in classes derived from QObject
    This is not relevant to the issue of implicit sharing.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedDate - possible misunderstanding

    I think there is a misunderstanding here. "Sharing" data is about sharing data between different instances of the data itself and not sharing data between different parts of the code. For the latter you just need a regular variable that different parts of the program have access to. So let's say you have an object representing a point (x,y) and you make a copy of that point (p2 = p1). Implicit sharing means that as long as you don't modify neither p1 nor p2 they use the same block of memory to keep the x and y values of both points. So you reduce memory usage and also reduce the time required to make a copy of the object. But if you modify either p1 or p2, the shared block is automatically duplicated and each instance (p1 and p2) gets its own copy that since then lives its own life. This is done by QSharedDataPointer.

    From what I understand what you want is to be able to access the same variable from different parts of your program -- effectively you want a global variable. Since "global variables are evil", find a way (e.g. by searching the forum) to get the same semantics without using global variables. QSharedData won't help you in that in any way.

    By the way, your QueryHandlerData is not derived from QObject.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #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: QSharedDate - possible misunderstanding

    Quote Originally Posted by wysota View Post
    By the way, your QueryHandlerData is not derived from QObject.
    yes, but classes which holds QSharedPointer are derived from QObject.
    If you pointed that out, that means there is a difference, but what exactly ?

    Quote Originally Posted by wysota View Post
    I think there is a misunderstanding here. "Sharing" data is about sharing data between different instances of the data itself and not sharing data between different parts of the code. For the latter you just need a regular variable that different parts of the program have access to. So let's say you have an object representing a point (x,y) and you make a copy of that point (p2 = p1). Implicit sharing means that as long as you don't modify neither p1 nor p2 they use the same block of memory to keep the x and y values of both points. So you reduce memory usage and also reduce the time required to make a copy of the object. But if you modify either p1 or p2, the shared block is automatically duplicated and each instance (p1 and p2) gets its own copy that since then lives its own life. This is done by QSharedDataPointer.

    From what I understand what you want is to be able to access the same variable from different parts of your program -- effectively you want a global variable. Since "global variables are evil", find a way (e.g. by searching the forum) to get the same semantics without using global variables. QSharedData won't help you in that in any way.
    Now I get it. I will check the forum and try to find what you told me to.
    By the way, currently I am still using static object of QueryHandlerDate (not static pointer) and my goal is achieved.
    I just wrote my own copy by reference constructor and I see that copying is not taking place whenever I use or modify something in it.
    Last edited by kornicameister; 8th February 2011 at 14:08.
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSharedDate - possible misunderstanding

    Quote Originally Posted by kornicameister View Post
    yes, but classes which holds QSharedPointer are derived from QObject.
    This doesn't make sense, you can't copy those. QSharedDataPointer points to a private data payload object which is not referenced from anywhere outside its public counterpart. QObjects are identity based, not value based, they are not fit for sharing.

    Again, "sharing" is different than "accessing". It means an object is owned by more than one object, not that it is accessed by more than one object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    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: QSharedDate - possible misunderstanding

    it couldn't be more confusing I guess...
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

Similar Threads

  1. Misunderstanding QTestLib's TestCase term
    By lyuts in forum Qt Programming
    Replies: 4
    Last Post: 13th July 2010, 11:34
  2. QSortFilterProxyModel.invalidateFilter() misunderstanding
    By aspidites in forum Qt Programming
    Replies: 2
    Last Post: 23rd April 2009, 13:17
  3. parent() heirarchy misunderstanding?
    By KShots in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2007, 18:18

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.