Results 1 to 7 of 7

Thread: Removing a value from QList via if statement check

  1. #1
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Exclamation Removing a value from QList via if statement check

    When a user clicks, the mouse position x & y value is stored in a QList<int>
    User will be able to click several times

    My problem is if a user clicks the same spot where he/she already clicked
    that x & y value must be deleted from the array only.

    This is so far what i got:
    Qt Code:
    1. //In mouseMove function
    2. if(ev->button()==Qt::LeftButton)
    3. {
    4. x1 = ev->x();
    5. y1 = ev->y(); //x1,y1 are temp variables later in same function used to .append into array
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    RemoveValue function is:
    Qt Code:
    1. void class::RemovePoint()
    2. {
    3. int r = 0;
    4. do
    5. {
    6. //Only 1 Array stored in format x|y|x|y|x|y|x|y|x|y|...
    7. if(Array1[r] == x1 && Array1[r+1] == y1)
    8. {
    9. Array1.removeAt(r);
    10. Array1.removeAt(r);
    11. }
    12. r = r + 2; //Jumps to next x value
    13. }while(r < 50); lets say 25 points, hence x & y values = 50 array elements
    14. }
    To copy to clipboard, switch view to plain text mode 

    I am not worried about the display
    Just need the array to be updated as it happens

    Program compiles
    when I test the same point
    Program crashes
    [Invalid parameter passing & Assert Failure :"index out of range"

    Help Appreciated...Much
    Thanks

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Removing a value from QList via if statement check

    When you start the loop the vector has 50 entries. But after removing two of them (one point), the list have only 48 and at the last loop you got the pretty clear assert because you asking for mode data that the container have.
    Use Array1.count() in your while test, it should work.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Removing a value from QList via if statement check

    Because after first use lines 9 and 10 size of Array1 is 48 not 50, after next 46 and so on. What type is Array1 ?

  4. #4
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Removing a value from QList via if statement check

    Any reason why you are not using QList<QPoint> or even QMap<int, int> ??

  5. #5
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Removing a value from QList via if statement check

    Tried .count()
    Now program does not crash with error message
    Just stops responding (I have to force close)
    Any ideas


    Using QList<int> because There are multiple arrays in my program each sub-categorized per type of calculation
    so my actual array is in the format x|y|result|... each array with different calculation results


    Thanks for the quick response

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Removing a value from QList via if statement check

    You should not advance r if you delete at r otherwise you will skip points.

    A list of structures is stil a better idea. if your "entry" is more than two integers then create a structure that holds all data per entry

    Qt Code:
    1. struct Entry
    2. {
    3. int x;
    4. int y;
    5. int result;
    6. };
    7.  
    8. QList<Entry> list;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    ebsaith (21st June 2013)

  8. #7
    Join Date
    Jun 2013
    Posts
    58
    Thanks
    26
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Removing a value from QList via if statement check

    Thanks
    Kind Regards

Similar Threads

  1. removing duplicate itens from QList<int>
    By john_god in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2014, 06:56
  2. help -- removing items from QList container
    By drinkwater in forum Newbie
    Replies: 4
    Last Post: 28th January 2012, 16:03
  3. How to get value from a query statement ?
    By hohoanganh205 in forum Newbie
    Replies: 3
    Last Post: 5th January 2012, 14:23
  4. Replies: 4
    Last Post: 20th August 2010, 13:54

Tags for this Thread

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.