Results 1 to 5 of 5

Thread: Problem replacing a value in a QList.

  1. #1
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem replacing a value in a QList.

    Hi,

    I've been trying to implement a function into my program that loads values from a .txt file. In the program that I've made there is a QList<QList<double>> (ie a QList containing QLists' of double values). Each QList<double> represents a 'layer', while the double values contained within the each seperate list represent layer properties (ie layer area). When the user closes the program after manipulating these properties (through QDoubleSpinBoxes that I've included on my form), the values are saved into a .txt file. When the user starts up the program again, the previous values are all reloaded. Here is the code:

    Qt Code:
    1. void ISocGenerator::getOldValues()
    2. {
    3. //inform user if old values file cannot be read from
    4. QFile oldValuesFile("lastSettings");
    5. if (!oldValuesFile.open(QIODevice::ReadOnly))
    6. {
    7. QMessageBox::information(this, tr("Unable to load previous values"),
    8. oldValuesFile.errorString());
    9. return;
    10. }
    11.  
    12. //create a text stream to read with
    13. QTextStream in(&oldValuesFile);
    14.  
    15. double theArray[4];
    16.  
    17. //get old number of layers
    18. int listSize;
    19. in >> listSize;
    20.  
    21. //get old X and Y dimensions
    22. double xDim;
    23. in >> xDim;
    24. ui.xSpinBox->setValue(xDim);
    25.  
    26. double yDim;
    27. in >> yDim;
    28. ui.ySpinBox->setValue(yDim);
    29.  
    30. //load old values
    31. for(int i = 0; i < listSize; i++)
    32. {
    33. //creates new lists for each loaded layer,
    34. //(except for the first layer, since it was already created)
    35. if(i != 0)
    36. {
    37. QList<double> newLayerData;
    38. addNewLayer();
    39. getNewLayerData(newLayerData);
    40. }
    41. //inserts the old values into the layer's list
    42. for(int j = 0; j < 4; j++)
    43. {
    44. in >> theArray[j];
    45.  
    46. (_allData.value(i)).replace(j, theArray[j]);
    47. }
    48.  
    49. //displayes values of first layer
    50. if(i == 0)
    51. {
    52. ui.doubleSpinBox->setValue(theArray[0]);
    53. ui.doubleSpinBox_2->setValue(theArray[1]);
    54. ui.doubleSpinBox_3->setValue(theArray[2]);
    55. ui.spinBox_4->setValue(theArray[3]);
    56. }
    57. }
    58. }
    To copy to clipboard, switch view to plain text mode 

    An example of the .txt file from which they are loaded would be:

    2 50 100 5000 4 1 7 3000 8 2 4

    Where the first value is the list size, the next two can be ignored, the next 4 are the values of the first layer, and the last 4 are the values of the second layer.

    The addNewLayer() function can be ignored.
    The getNewLayerData() function inserts ten '0's into 'newLayerData'.

    The first layer's values are all correctly loaded but the remaining layers all have zeros for their values. Line 46 is where I'm having the problem. It should be replacing the first 4 values of a given list, with the values that have been loaded into 'theArray' however it just replaces them with zeros. When I debug, it shows me that the values loaded into theArray are always correct, however it seems that the replacing function is inserting zeros rather than the values in theArray.

    I'm still really new to programming in general, so I'm having trouble catching my error.

    What am I doing wrong?
    Last edited by bbad68; 28th January 2010 at 22:55.

  2. #2
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Problem replacing a value in a QList.

    Try this at 46

    (_allData.value(i)).at(j) = theArray[j];
    BTW what us _allData? is it list of list ?

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

    bbad68 (29th January 2010)

  4. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    518
    Thanks
    13
    Thanked 77 Times in 75 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem replacing a value in a QList.

    Hi, I think the problem is that _allData.value(i) does not return a reference but a copy of the object at position i so you just change that copy and not the original data. Try the [] operator instead of the value() method.

    Ginsengelf

  5. The following user says thank you to Ginsengelf for this useful post:

    bbad68 (29th January 2010)

  6. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem replacing a value in a QList.

    QList::value() and QList::at() functions return a copy.
    Use [] operator instead to refer the actual item.

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

    bbad68 (29th January 2010)

  8. #5
    Join Date
    Jan 2010
    Posts
    19
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem replacing a value in a QList.

    Thanks guys, that worked perfectly!

Similar Threads

  1. (Solved) Removing (replacing) central widget
    By frenk_castle in forum Newbie
    Replies: 7
    Last Post: 14th December 2010, 10:02
  2. Replies: 0
    Last Post: 4th March 2009, 19:27
  3. problem with QList
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2008, 13:08
  4. Replacing Central Widget in Designer
    By Max Yaffe in forum Qt Tools
    Replies: 2
    Last Post: 11th July 2007, 11:41
  5. Replacing One image with another
    By merry in forum Qt Tools
    Replies: 6
    Last Post: 8th February 2007, 13:22

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
  •  
Qt is a trademark of The Qt Company.