Results 1 to 3 of 3

Thread: Chancing values to a QList<object>

  1. #1
    Join Date
    May 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Chancing values to a QList<object>

    Hi, I'm new to QT and c++ and need some help. Im making a application for circuit board spesifications.

    I have a class Panel which have a list of boards:QList<Board>, and in the Board class i have a Qlist of Layers, Qlist<Layer>. Im trying to modify my layer class values through two QLists, but the values wont budge. any idea?

    Qt Code:
    1. //layer.h
    2. private:
    3. QString type;
    4. public:
    5. QString getType();
    6. void setType(QString newType);
    7.  
    8. //layer.cpp
    9. void layer::setType(QString newType){newType = type;}
    10. QString layer::getType(){return type;}
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //board.h
    2. private:
    3. QList<layer> layers;
    4. public:
    5. void addLayer();
    6. QList<layer> getLayers();
    7.  
    8. //board.cpp
    9. QList<layer> Board::getLayers(){return layers;}
    10. void Board::addLayer(){
    11. layer new1;
    12. layers.append(new1);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //panel.h
    2. QList<Board> b;
    3. Board testBoard;
    4.  
    5. //panel.cpp
    6. b.append(testBoard);
    7. b.operator [](0).addLayer(); //adding a layer is no problem
    8. b.operator [](0).getLayers().operator [](0).setType("nooo");
    9. //trying to set the value, but nothing happends.
    10. QString check = b.value(0).getLayers().value(0).getType();
    11. //the check value returns the initial value of type from the constructor.
    To copy to clipboard, switch view to plain text mode 

    I appreciate any help. Thx
    Terje H

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Chancing values to a QList<object>

    Your problems are basic C++ understanding rather than any thing Qt specific.

    Line 6 of listing 2 tells me that your getLayers() returns a copy of the list of layers rather than a reference/pointer to the existing one. When you use that at line 8 of the last listing you are operating on a transient copy of the list which then is discarded. When you check the value at line 10 you are making another copy of the original, checking and then discarding it.

    Lines 7, 8 and 10 in the last listing are using operator[] in a particularly odd way also.

    Try this instrumented code (with explicit copy constructor) and pay particular attention to which layer objects you are accessing:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. class layer {
    5. QString type;
    6. public:
    7. layer(): type("default") {
    8. qDebug() << "Construct" << this;
    9. }
    10. layer(const layer& other) {
    11. qDebug() << "Copy construct" << this;
    12. type = other.type;
    13. }
    14. ~layer() {
    15. qDebug() << "Destruct" << this;
    16. }
    17. QString getType() const {
    18. qDebug() << "getType" << type << this;
    19. return type;
    20. }
    21. void setType(QString newType) {
    22. qDebug() << "setType" << newType << this;
    23. type = newType;
    24. }
    25. };
    26.  
    27. class board {
    28. QList<layer> layers;
    29. public:
    30. QList<layer> getLayers() {return layers;}
    31. //QList<layer>& getLayers() {return layers;}
    32. void addLayer() {
    33. qDebug() << "start adding layer";
    34. layer newLayer;
    35. layers.append(newLayer);
    36. qDebug() << "finish adding layer";
    37. }
    38.  
    39. };
    40.  
    41. int main(int argc, char *argv[])
    42. {
    43. QCoreApplication app(argc, argv);
    44.  
    45. QList<board> b;
    46. board testBoard;
    47. b.append(testBoard);
    48. b[0].addLayer();
    49. b[0].getLayers()[0].setType("Hi");
    50. qDebug() << b[0].getLayers().at(0).getType();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Then change getLayers() to return "QList<layer>&" and look at the difference.

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

    terhje (19th May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Chancing values to a QList<object>

    Thanks for taking the time to help me ChrisW67.

Similar Threads

  1. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. QList & QPointer to store object list
    By maddog_fr in forum Qt Programming
    Replies: 12
    Last Post: 8th August 2009, 20:39
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  5. Values not being appended in a QList
    By Kapil in forum Newbie
    Replies: 5
    Last Post: 21st April 2006, 10:20

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.