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:
#include <QApplication>
#include <QDebug>
#include <QObject>
{
public:
bool operator==(const CustomClass& other)const
{
if(this->name==(other.name))
{
return true;
}
return false;
}
};
class CustomClassPtr{
public:
CustomClassPtr(CustomClass * ptr) : _ptr(ptr){
}
CustomClass * _ptr;
};
bool operator==(const CustomClassPtr& p1, const CustomClassPtr& p2){
return *p1._ptr == *p2._ptr;
}
int main(int argc, char **argv)
{
QList <CustomClassPtr>list;
CustomClass *c1=new CustomClass();
c1->name= "john";
list.append(c1);
CustomClass *c2=new CustomClass();
c2->name= "john";
qDebug()<<list.contains(c2); //true
return 0;
}
#include <QApplication>
#include <QDebug>
#include <QObject>
class CustomClass : public QObject
{
public:
QString name;
bool operator==(const CustomClass& other)const
{
if(this->name==(other.name))
{
return true;
}
return false;
}
};
class CustomClassPtr{
public:
CustomClassPtr(CustomClass * ptr) : _ptr(ptr){
}
CustomClass * _ptr;
};
bool operator==(const CustomClassPtr& p1, const CustomClassPtr& p2){
return *p1._ptr == *p2._ptr;
}
int main(int argc, char **argv)
{
QList <CustomClassPtr>list;
CustomClass *c1=new CustomClass();
c1->name= "john";
list.append(c1);
CustomClass *c2=new CustomClass();
c2->name= "john";
qDebug()<<list.contains(c2); //true
return 0;
}
To copy to clipboard, switch view to plain text mode
I skipped the pointer checks etc. just to present the idea.
Bookmarks