Results 1 to 4 of 4

Thread: Why changed values in a foreach don't save?

  1. #1
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Why changed values in a foreach don't save?

    I don't belive in my eyes. I wrote a foreach to change a bool value... (bool FileEntry.selected)

    Qt Code:
    1. int i=-1;
    2. foreach (FileEntry itmFile, fileList)
    3. {
    4. i++;
    5. if((tmpType=="alle")||(itmFile.type==tmpType)) //Stimmt Typ?
    6. {
    7. if(itmFile.size+currentSize<maxSize) //Begrenzung noch nicht überschritten?
    8. {
    9. currentSize += itmFile.size;
    10. //itmFile.selected=true; //wird ausgewählt
    11. fileList[i].selected=true;
    12. continue;
    13. }
    14. }
    15. //itmFile.selected=false; //wird nicht ausgewählt
    16. fileList[i].selected=false;
    17. }
    To copy to clipboard, switch view to plain text mode 

    ..and the (now) commted entries like "itmFile.selected=true" change the value only for/in the foreach! After the foreach is done, the value is false, like before. But when i use something like "fileList[i].selected=true", the value is after leaving the foreach is still 'true'. Is that normal???


    thx.cit

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why changed values in a foreach don't save?

    This is quite confusing piece of code. You are using "for-each" type of loop, but you are accessing the iterated list via index at the same time. This is not self-explanatory and I strongly recommend not to write such code.
    Either just use simple "for (int i=0...)" type of loop, or stick to iterator-based foreach.
    // edit: I guess you did that only for tests, so forget above comment

    "itmFile.selected=true" change the value only for/in the foreach
    Of course it is, since "itmFile" is a copy of the value from list:
    Qt Code:
    1. foreach (FileEntry itmFile, fileList)
    To copy to clipboard, switch view to plain text mode 
    On each iteration you assign a copy from "fileList", roughly it is equivalent to
    Qt Code:
    1. for (...){
    2. FileEntry itmFile = fileList[i];
    3. }
    To copy to clipboard, switch view to plain text mode 
    If you want to iterate and change a sequence via foreach loop, use references:
    Qt Code:
    1. // change to itmFile will affect objects from fileList
    2. foreach (FileEntry & itmFile, fileList)
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 25th July 2014 at 23:45.

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

    cit (25th July 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    17
    Thanks
    1

    Default Re: Why changed values in a foreach don't save?

    Yes, i do this (stupid) iteration-stuff only for testing ;-)

    Thanks, you give me the solution. Sorry i normaly work with C#. At C# it's not a "copy". okay, i will do it by ref, because i like the 'foreach' ;-)

    PS: what a stumbling block!!!!


    Added after 26 minutes:


    I tried the "&", but i got an error.

    Qt Code:
    1. // int i=-1;
    2. foreach (FileEntry & itmFile, fileList)
    3. {
    4. //i++;
    5. if((tmpType=="alle")||(itmFile.type==tmpType)) //Stimmt Typ?
    6. {
    7. if(itmFile.size+currentSize<maxSize) //Begrenzung noch nicht überschritten?
    8. {
    9. currentSize += itmFile.size;
    10. itmFile.selected=true; //wird ausgewählt
    11. //fileList[i].selected=true;
    12. continue;
    13. }
    14. }
    15. itmFile.selected=false; //wird nicht ausgewählt
    16. //fileList[i].selected=false;
    17. }
    To copy to clipboard, switch view to plain text mode 


    /usr/include/qt5/QtCore/qglobal.h:860: error: invalid initialization of reference of type 'FileEntry&' from expression of type 'const FileEntry'
    for (variable = *_container_.i;; __extension__ ({--_container_.brk; break;}))


    For your information:
    Qt Code:
    1. QList<FileEntry> fileList;
    To copy to clipboard, switch view to plain text mode 
    (no pointer)
    Last edited by cit; 26th July 2014 at 00:25.

  5. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Why changed values in a foreach don't save?

    Of course, sorry, my bad, I'm just used to C++11 style "for" loops:
    Qt Code:
    1. for (FileEntry & itmFile : fileList){
    2. //...
    3. }
    To copy to clipboard, switch view to plain text mode 
    I forgot the fact that Qt "foreach" makes a copy of the container before entering the loop (but it is cheap thanks to implicit sharing).
    Above "for" loop will work as expected.
    In order to use it you need to have
    1) a compiler that supports c++11 features
    2) if not enabled by default, you need to add this to the <project>.pro file:
    Qt Code:
    1. // .pro
    2. CONFIG += c++11
    To copy to clipboard, switch view to plain text mode 
    Here is a small example:
    Qt Code:
    1. #include <QList>
    2. #include <QDebug>
    3.  
    4. struct data_t{
    5. int value;
    6. data_t(int a=0) : value(a){}
    7. };
    8.  
    9. QDebug& operator<< (QDebug& stream, const data_t& d){
    10. stream << d.value;
    11. return stream;
    12. }
    13.  
    14.  
    15. int main(){
    16. QList<data_t> list;
    17. list << data_t(1) << data_t(2) << data_t(3);
    18. qDebug() << list;
    19. for (data_t& d : list){
    20. d.value += 1;
    21. }
    22. qDebug() << list;
    23. return 0;
    24. }
    25.  
    26. // should output:
    27. // 1, 2, 3)
    28. //(2, 3, 4)
    To copy to clipboard, switch view to plain text mode 
    Sorry for the confusion.

Similar Threads

  1. foreach or alike
    By prophet0 in forum Qt Programming
    Replies: 18
    Last Post: 5th January 2012, 17:03
  2. foreach error while compiling
    By smanoj in forum Newbie
    Replies: 4
    Last Post: 24th November 2011, 05:32
  3. lifetime of foreach
    By BalaQT in forum Newbie
    Replies: 4
    Last Post: 4th March 2010, 15:55
  4. Foreach performance
    By jano_alex_es in forum General Programming
    Replies: 2
    Last Post: 17th November 2009, 13:26
  5. SQL changed values stored?
    By jmqt in forum Newbie
    Replies: 1
    Last Post: 15th June 2009, 17:27

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.