Results 1 to 2 of 2

Thread: Change a QList element

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Qt products
    Qt4

    Default Change a QList element

    Hi I have a ?

    I do like this in the *.h file


    Qt Code:
    1. typedef struct{
    2. QRect dataRect;
    3. QString imgName;
    4. int id;
    5. } image;
    6.  
    7.  
    8. QList<image> list;
    To copy to clipboard, switch view to plain text mode 

    this in the cpp file.
    Qt Code:
    1. image myImage;
    2. myImage.dataRect=this->rect();
    3. myImage.id=0;
    4. myImage.imgName="b.png";
    5. list.append(myImage);
    6.  
    7. myImage.dataRect=QRect(10,400,167,64);
    8. myImage.id=1;
    9. myImage.imgName="b2.png";
    10. list.append(myImage);
    11.  
    12. myImage.dataRect=QRect(185,400,167,64);
    13. myImage.id=2;
    14. myImage.imgName="b3.png";
    15. list.append(myImage);
    To copy to clipboard, switch view to plain text mode 

    Now I would like to change the image name.
    but it does not change in the list why ?



    Qt Code:
    1. image myImage=list.at(a);
    2.  
    3.  
    4.  
    5. if (myImage.dataRect.contains(e->pos()))
    6. {
    7. id=a;
    8. switch (a)
    9. {
    10. case 0: break;
    11. case 1: break;
    12. case 2: myImage.imgName="b10.png";break;
    13. case 3: myImage.imgName="b12.png";break;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 25th August 2009 at 09:45. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Change a QList element

    You're changing only the value in the local variable myImage.

    To change in the list you need to have a pointer to the right image in the list:

    Qt Code:
    1. image *myImage=&(list.at(a));
    2.  
    3. if (myImage->dataRect.contains(e->pos()))
    4. {
    5. id=a;
    6. switch (a)
    7. {
    8. case 0: break;
    9. case 1: break;
    10. case 2: myImage->imgName="b10.png";break;
    11. case 3: myImage->imgName="b12.png";break;
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 19:55
  2. QList, copy problems
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 5th February 2010, 01:06
  3. QList inside a QList
    By jano_alex_es in forum Newbie
    Replies: 2
    Last Post: 1st July 2009, 12:59
  4. Change cursor & status during Drag & Drop
    By ronlongo in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2008, 17:56
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 21:43

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.