Results 1 to 5 of 5

Thread: help -- removing items from QList container

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help -- removing items from QList container

    Hi, this is my first time posting to this forum. I'm definitely a noob at both QT and c++, but with that said, I'm really liking both. Some of the details are killing me though. My problem is that I cannot delete an object from a QList Container. I can append just fine. Please see my code below.
    ---------------------------
    Qt Code:
    1. class Contact{
    2. public:
    3. Contact(int cat, QString fn, QString ln,
    4. QString sa, QString zc, QString cityv,QString pn ); //Contact cstor
    5.  
    6. int category;
    7. QString firstName;
    8. QString lastName;
    9. QString streetAddress;
    10. QString zipCode;
    11. QString city;
    12. QString phoneNumber;
    13.  
    14. public:
    15. QString toString(){}
    16. //I don't know what this method is for
    17. };
    18.  
    19. class ContactList{ //define class
    20. public:
    21. ContactList(); //constructor defined
    22.  
    23. QList<Contact> List; //this holds everything -- QList container class
    24.  
    25. void add(Contact c){
    26. List.append(c); //this works just fine!
    27. }
    28.  
    29. void remove(Contact c){
    30.  
    31. List.removeAll(c); //this does not work <-------------this is my problem line!
    32. }
    33. };
    To copy to clipboard, switch view to plain text mode 
    ---------------------
    I've tried putting combinations of & and * on the parameters thinking maybe i need to use
    a reference instead of actually passing an object, but nothing works. Thank you for any help
    in this matter. I'm sure there is a very basic principle I'm missing, but as i said, I'm still learning.

    oops, just noticed when i pasted text in here i have an extra "}" in there....that's just a typo and is not the cause of my problem. It definitely has to do with the fact that i'm trying to remove a class object from List

    -Ryan
    Last edited by wysota; 28th January 2012 at 03:56. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: help -- removing items from QList container

    I think the problem is you are missing an implementation for the equals operator (operator==) for your class. QList is unable to compare items it holds.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help -- removing items from QList container

    I'm sorry, I don't know what that means...i feel a noob smack is coming. I put "operator==" text in which class and what does it do? Thanks for any info you can give me.

  4. #4
    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: help -- removing items from QList container

    operator==() is either a member function of Contact or a free function that compares two Contacts and return a true/false indication of equality.
    The function prototype is one of these:
    Qt Code:
    1. bool operator==(const Contact &rhs) { ... } // member function version
    2. OR
    3. bool operator==(const Contact &lhs, const Contact &rhs) { ... } // free function version
    To copy to clipboard, switch view to plain text mode 

    You want to look at "operator overloading" in your favourite C++ book. If you don't have one then try at Stack Overflow or http://www.mindview.net/Books/TICPP/...ngInCPP2e.html (there is a freely downloadable version).

  5. #5
    Join Date
    Jan 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: help -- removing items from QList container

    Thank you very much! That worked.

Similar Threads

  1. removing items QTreeWidget
    By Mystical Groovy in forum Qt Programming
    Replies: 3
    Last Post: 25th March 2015, 22:58
  2. removing duplicate itens from QList<int>
    By john_god in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2014, 07:56
  3. Replies: 3
    Last Post: 7th August 2009, 11:21
  4. Removing items from QtableWidget
    By algajard in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 10:31
  5. Removing items from qtreeview
    By Babak in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2009, 20:44

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.