Results 1 to 3 of 3

Thread: Qt Container do not save items

  1. #1
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Qt Container do not save items

    Hello,

    i try to model the relationshipe between Persons in a Group.

    The code looks like this:


    dialog.h
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. #include "group.h"
    7. #include "person.h"
    8.  
    9.  
    10. namespace Ui {
    11. class Dialog;
    12. }
    13.  
    14. class Dialog : public QDialog
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit Dialog(QWidget *parent = 0);
    20. ~Dialog();
    21.  
    22. void test1();
    23.  
    24. private:
    25. Ui::Dialog *ui;
    26. };
    27.  
    28. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp

    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9.  
    10. test1();
    11.  
    12.  
    13. }
    14.  
    15. Dialog::~Dialog()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void Dialog::test1()
    21. {
    22. //create a group
    23. Group group;
    24.  
    25. //create two persons
    26. Person pete = group.addPersonToGroup("Pete");
    27. Person mary = group.addPersonToGroup("Mary");
    28.  
    29. // create relationship
    30. group.knows(pete,"friends",mary);
    31.  
    32. //if the debugger run to this point,
    33. // the member variable mFriends. is empty
    34. // i have expected:
    35. // pete.mFriends contains mary
    36. // mary.mFriends contains pete
    37. int i = 0;
    38. }
    To copy to clipboard, switch view to plain text mode 


    group.h
    Qt Code:
    1. #ifndef GROUP_H
    2. #define GROUP_H
    3.  
    4. #include <QString>
    5.  
    6. #include "person.h"
    7.  
    8. class Group
    9. {
    10. public:
    11. Group();
    12. Person addPersonToGroup(QString name);
    13. void knows(Person p1, QString label, Person p2);
    14. };
    15.  
    16. #endif // GROUP_H
    To copy to clipboard, switch view to plain text mode 


    group.cpp
    Qt Code:
    1. #include "group.h"
    2.  
    3. Group::Group()
    4. {
    5. }
    6.  
    7. Person Group::addPersonToGroup(QString name)
    8. {
    9. Person newPerson;
    10. newPerson.setName(name);
    11.  
    12. return newPerson;
    13. }
    14.  
    15. void Group::knows(Person p1, QString label, Person p2)
    16. {
    17.  
    18. p1.knows(label, p2);
    19. }
    To copy to clipboard, switch view to plain text mode 

    person.h
    Qt Code:
    1. #ifndef PERSON_H
    2. #define PERSON_H
    3.  
    4. #include <QString>
    5.  
    6. #include "personset.h"
    7.  
    8.  
    9. class PersonSet;
    10.  
    11. class Person
    12. {
    13. public:
    14. Person();
    15.  
    16. void setName(QString name);
    17. void knows(QString label, Person p1);
    18.  
    19. private:
    20. QString mName ;
    21. QHash<QString,PersonSet> mFriends;
    22. };
    23.  
    24. #endif // PERSON_H
    To copy to clipboard, switch view to plain text mode 


    person.cpp
    Qt Code:
    1. #include "person.h"
    2.  
    3. Person::Person()
    4. {
    5. }
    6.  
    7. void Person::setName(QString name)
    8. {
    9. mName = name;
    10. }
    11.  
    12. void Person::knows(QString label,Person p1)
    13. {
    14. PersonSet personSet = mFriends[label];
    15.  
    16. if(personSet.empty())
    17. {
    18. // here is the insert of person into mFriends/ QHash
    19. personSet.insert(0,p1);
    20. mFriends.insert(label,personSet);
    21. }
    22.  
    23. personSet.insert(0,p1);
    24.  
    25. if(!label.startsWith("_back_"))
    26. {
    27. p1.knows("_back_"+label,*this);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    int
    personSet.h
    Qt Code:
    1. #ifndef PERSONSET_H
    2. #define PERSONSET_H
    3.  
    4. #include <QHash>
    5.  
    6. #include "person.h"
    7.  
    8. class Person;
    9.  
    10. class PersonSet : public QHash<int,Person>
    11. {
    12. public:
    13. PersonSet();
    14. };
    15.  
    16. #endif // PERSONSET_H
    To copy to clipboard, switch view to plain text mode 

    personSet.cpp
    Qt Code:
    1. #include "personset.h"
    2.  
    3. PersonSet::PersonSet()
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

    Like descripted in the conments of the function Dialog::test1(), the mFriends variable of the typ personSet container class which inherits from QHash<int,Person> is empty after the function Dialog::test1() passes through.

    But i do get it why ?

    Thx

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Container do not save items

    But i do get it why ?
    In this method:

    Qt Code:
    1. void Group::knows(Person p1, QString label, Person p2)
    To copy to clipboard, switch view to plain text mode 

    you are passing all of the arguments by value, not by reference. This means that p1 and p2 are temporary copies of "pete" and "mary", not the actual "pete" and "mary" instances themselves. Inside the method, these temporary Person instances are made friends of each other, and as soon as the method exits, the temporaries go out of scope and they aren't friends any more In fact, they don't even exist. Your original instances of "pete" and "mary" are never modified, just their copies.

    If you want this to work as you expect, you need to learn how passing arguments by reference works. Your code should look like this instead:

    Qt Code:
    1. void Group::knows(Person & p1, const QString & label, Person & p2)
    To copy to clipboard, switch view to plain text mode 

    The "const QString &" declaration is good practice. The "const" tells the compiler that "label" won't be modified inside the method, and the pass by reference ("&") tells the compiler it doesn't need to copy "label", it just passes its address.

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

    Andre008 (24th February 2016)

  4. #3
    Join Date
    Feb 2016
    Posts
    17
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Container do not save items

    This was the issue
    Thx

Similar Threads

  1. help -- removing items from QList container
    By drinkwater in forum Newbie
    Replies: 4
    Last Post: 28th January 2012, 16:03
  2. How to save QGraphicsScene Items to a file?
    By yagabey in forum Qt Programming
    Replies: 5
    Last Post: 28th February 2010, 10:16
  3. Replies: 3
    Last Post: 7th August 2009, 10:21
  4. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03

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.