に含まれています。あなただけの比較メソッドをオーバーライドする必要がJavaでQt- QListにチェックがカスタムクラスをロードのQt QListにの比較メカニズムを無効にする方法はありますカスタムクラス
すなわち。
私はcustomclassモデルとQListのを持っています。
QList <CustomClass *>list;
CustomClass *c1=new CustomClass();
c1.name= "john";
list.append(c2);
CustomClass *c2=new CustomClass();
c2.name= "john";
qDebug()<<list.contains(c2); //false
//Secondly I have overridden the equals '==' operator and still getting false
qDebug()<< (c1 == c2); //false, why ?
qDebug()<< (c1->operator ==(*c2)); //true
class CustomClass
{
QString name;
bool operator==(const CustomClass& other)const
{
if(this->name==(other.name))
{
return true;
}
return false;
}
}
1つの変更は、QObjectを継承しません。私がQObjectから継承するときQList containsは機能しません。 (上記の私のコードが同じであると仮定すると) – user609530
はい、 'QObject'派生クラスのインスタンスはコンテナの値として使用できません。 'QObject'を格納する必要がある場合は、それらにポインタを格納します。 – laalto