Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QVariant comparator



  • Hi,

    I'm declaring a class as given below

    @
    class Foo : public Object
    {
    Q_OBJECT
    public:
    Foo(Object* parent = 0);
    virtual ~Foo();

     void setName(QString name);
     QString name();
    

    private:
    QString m_name;
    };

    inline bool operator==(const Foo& lhs, const Foo& rhs)
    {
    qDebug() << "entered Foo == operator";
    return (lhs.name() == rhs.name());
    }

    inline bool operator<(const NameProperty& lhs, const NameProperty& rhs)
    {
    return lhs.name() < rhs.name();
    }

    Q_DECLARE_METATYPE(Foo*)
    @

    and registered it this way

    qRegisterMetaType<Foo*>();
    QMetaType::registerComparators<Foo*>();

    however the equality operator never get's called when i compare two QVariants that contain Foo*.

    Please advise what I'm doing wrong.

    [edit: added missing coding tags @ SGaist]


  • Lifetime Qt Champion

    Hi,

    AFAIK, you are providing comparators for Foo not Foo*, so it's comparing the pointer values.

    On a side note, please provide correct code sample, this one doesn't allow to test your problem


Log in to reply