Results 1 to 12 of 12

Thread: QList contains problem with custom objects

  1. #1
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default QList contains problem with custom objects

    Hi,

    I have Qlist with Customclass objects. When I check the QList with 'contains' method using a Custom class reference it returns false.I have also overridden the 'operator=='.

    I know that I am using pointers to compare rather than value.

    What should I do to achieve this.

    Qt Code:
    1. QList <CustomClass *>list;
    2. CustomClass *c1=new CustomClass();
    3. c1.name= "john";
    4. list.append(c2);
    5. CustomClass *c2=new CustomClass();
    6. c2.name= "john";
    7.  
    8. qDebug()<<list.contains(c2); //false
    9.  
    10. class CustomClass : public QObject
    11. {
    12. QString name;
    13. bool operator==(const CustomClass& other)const
    14. {
    15. if(this->name==(other.name))
    16. {
    17. return true;
    18. }
    19. return false;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 


    One more thing.
    I cannot and don't want to have QList <CustomClass>list, because QObject does not allow it (unless I override constructors).
    Last edited by saravanadel; 25th January 2012 at 09:33. Reason: wrapped code tags

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList contains problem with custom objects

    The List does not contain c2 because c2 does not point to the same object as c1. Create a free function

    Qt Code:
    1. bool operator==(const CustomClass* lhs, const CustomClass* rhs)
    2. {
    3. return *lhs == *rhs;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Are you sure that QList uses operator==? Maybe you need to define operator<...

    Felix

  3. #3
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: QList contains problem with custom objects

    Quote Originally Posted by FelixB View Post
    The List does not contain c2 because c2 does not point to the same object as c1. Create a free function

    Qt Code:
    1. bool operator==(const CustomClass* lhs, const CustomClass* rhs)
    2. {
    3. return *lhs == *rhs;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Are you sure that QList uses operator==? Maybe you need to define operator<...

    Felix
    When I declare this it says , 'bool operator== (const CustomClass* lhs, const CustomClass* rhs) must have an argument of class or enumerated type'

  4. #4
    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: QList contains problem with custom objects

    This won't work. Since c1 and c2 are different objects, comparing pointers will compare their addresses which are different. Your operator will not be used at all.

    You need to declare the operator for comparing pointers (as already advised) and you need to do that in a place where your class is visible (like right after the semicolon ending your class definition).
    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.


  5. #5
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: QList contains problem with custom objects

    When I declare this it says , 'bool operator== (const CustomClass* lhs, const CustomClass* rhs) must have an argument of class or enumerated type'

    How I declared,
    Qt Code:
    1. //CustomClass.h
    2. class CustomClass
    3. {
    4. };
    5. bool operator==(const CustomClass* lhs, const CustomClass* rhs)
    6. {
    7. return *lhs == *rhs;
    8. }
    9. //End of header
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: QList contains problem with custom objects

    In that case you can't override the operator for comparing pointers. The only thing you can do is to check whether QList accepts an additional parameter acting as a comparator for items it holds.

    Edit: no, it doesn't.
    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.


  7. #7
    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: QList contains problem with custom objects

    When I declare this it says (...)
    I don't think you can do that in C++. Pointers are built-in types, they have standard behavior for operations like <,>,== etc. It's like you want to overload operator == for integers, compiler should not allow you to do that.
    Why do you want to store pointers in the QList, can't you just use QList<CustomClass> ?

    @down:
    right, I missed that, because of the code from post #5.
    Last edited by stampede; 25th January 2012 at 11:57.

  8. #8
    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: QList contains problem with custom objects

    He can't because the class inherits QObject. The real question is whether it really has to inherit QObject.
    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.


  9. #9
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: QList contains problem with custom objects

    Does this boil down to a limitation. What I mean is take language objective-c, java, python for example. Searching a list should be easy.

    Here I want to use the Qt QObject model, but I cant use pointers in QList (becoz they cant search with pointers).

  10. #10
    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: QList contains problem with custom objects

    Searching a list is easy. But if the list contains apples and you try to search it for oranges, you will find nothing of interest. All those languages you mentioned have no notion of pointers, by the way.
    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.


  11. #11
    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: QList contains problem with custom objects

    I agree with wysota - searching a list works perfectly fine as long as you know what you are doing.
    Here I want to use the Qt QObject model, but I cant use pointers in QList (becoz they cant search with pointers).
    Probably you can get away with simple wrapper class, but this is not very elegant solution:
    Qt Code:
    1. #include <QApplication>
    2. #include <QDebug>
    3. #include <QObject>
    4.  
    5. class CustomClass : public QObject
    6. {
    7. public:
    8. QString name;
    9. bool operator==(const CustomClass& other)const
    10. {
    11. if(this->name==(other.name))
    12. {
    13. return true;
    14. }
    15. return false;
    16. }
    17. };
    18.  
    19. class CustomClassPtr{
    20. public:
    21. CustomClassPtr(CustomClass * ptr) : _ptr(ptr){
    22. }
    23. CustomClass * _ptr;
    24. };
    25.  
    26. bool operator==(const CustomClassPtr& p1, const CustomClassPtr& p2){
    27. return *p1._ptr == *p2._ptr;
    28. }
    29.  
    30. int main(int argc, char **argv)
    31. {
    32. QList <CustomClassPtr>list;
    33. CustomClass *c1=new CustomClass();
    34. c1->name= "john";
    35. list.append(c1);
    36. CustomClass *c2=new CustomClass();
    37. c2->name= "john";
    38.  
    39. qDebug()<<list.contains(c2); //true
    40.  
    41. return 0;
    42. }
    To copy to clipboard, switch view to plain text mode 
    I skipped the pointer checks etc. just to present the idea.

  12. #12
    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: QList contains problem with custom objects

    One can also use QSharedPointer and/or QWeakPointer or even QPointer (alas it's deprecated).

    By the way, searching the list this way (I mean using QList::contains) is not a good idea. The complexity is then O(n). Either presort the list and use qBinaryFind (using a dedicated LessThan implementation so that you don't have to use any pointer wrappers) or use QHash and search by key (you don't need any extra treatment then). You'll get O(logn) then.
    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.


Similar Threads

  1. QList custom data format
    By sureshkvl in forum Qt Programming
    Replies: 1
    Last Post: 13th October 2010, 13:26
  2. Replies: 4
    Last Post: 30th August 2010, 07:28
  3. Replies: 0
    Last Post: 25th June 2009, 08:17
  4. Custom objects in a QGraphicScene
    By draand in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 11:31
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43

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.