Results 1 to 6 of 6

Thread: Access to shared map between parent - children

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Access to shared map between parent - children

    Hi,

    In my applications I have class which contains QMap, and several child classes which execute tasks on the variables from this map.

    Now I would ask you, which way for communication between parent - child is a better in this case:
    - cast the parent pointer to the right type and then call parent function
    - store the pointer to the parent somewhere in the child when receiving it in the constructor

    or use signals and slots - send signal to parent with key as argument and then wait for value from map?
    But with signals and slots is a problem, because when child send signal with key to parent class then parent should send signal with value to only specific child object but signal is send to all child.

    Which way you choose when you have a similar problem?
    Thanks,
    Last edited by atomic; 10th August 2015 at 18:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to shared map between parent - children

    Either keep a pointer to the parent or share the map.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to shared map between parent - children

    I can not share the map because it must be protected from simultaneously access at the same time something like this

    Qt Code:
    1. int accessToMap(QString key)
    2. {
    3. QMutexLocker locker(&mutex);
    4. //.......
    5.  
    6. return mMap.value(key);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Either keep a pointer to the parent or share the map.
    Do you mean keep pointer inside class and initialize it inside constructor?

    Qt Code:
    1. class Child {
    2.  
    3. };
    4.  
    5. class Parent {
    6.  
    7. public:
    8. Parent(Child * child) : mChild(child) {};
    9. private:
    10. Child * mChild;
    11. };
    To copy to clipboard, switch view to plain text mode 

    or use something like that

    Qt Code:
    1. class Parent : public QObject{
    2.  
    3. public:
    4. Parent() {
    5. child = new Child(this);
    6. }
    7.  
    8. int accessToMap(QString key) {};
    9.  
    10. private:
    11. Child *child;
    12. };
    13.  
    14. class Child : public QObject{
    15.  
    16. public:
    17. Child(QObject *parent) : QObject(parent){};
    18.  
    19. void accessToMap(QString key ) {
    20. Parent* parent = qobject_cast<Parent*>(this->parent());
    21. parent->accessToMap("somekey");
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to shared map between parent - children

    Ah, you didn't say the the children are used by different threads, nor that the map is modified during runtime.

    You could still pass a synchronized container (e.g. wrapper around the map) but option 1 (explicitly typed member) looks good as well.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to shared map between parent - children

    But it looks like that, I store duplicate a pointer to parent, because QObject already has it and return it via parent() method. I am right?

    Whether first method is better in multithreading app from that

    Qt Code:
    1. Parent* parent = qobject_cast<Parent*>(this->parent());
    2. parent->accessToMap("somekey");
    To copy to clipboard, switch view to plain text mode 
    or it is the same?

    Thanks,

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Access to shared map between parent - children

    Sure, if you want to lose the type safety or incure the runtime overhead of a checked cast, that's your choice.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    atomic (11th August 2015)

Similar Threads

  1. Replies: 4
    Last Post: 4th March 2014, 19:28
  2. Replies: 6
    Last Post: 22nd December 2011, 21:03
  3. Replies: 0
    Last Post: 14th April 2010, 15:03
  4. disable a parent and children in a tree model
    By qt_gotcha in forum Newbie
    Replies: 4
    Last Post: 9th July 2009, 06:49

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.