Using custom type in Q_PROPERTY

766 views Asked by At

I couldn't use custom type in Q_PROPERTY MEMBER.

header.h

class Custom {
  Q_GADGET
  Q_PROPERTY(int mode MEMBER mode STORED true)

 public:
  Custom();
  int mode;
};

class Test {
  Q_GADGET
  Q_PROPERTY(QString ip MEMBER ip STORED true)
  Q_PROPERTY(Custom discovery MEMBER discovery STORED true)

 public:
  Test();
  QString ip;
  Custom discovery;
};

Q_DECLARE_METATYPE(Custom)
Q_DECLARE_METATYPE(Test)

main.cpp

#include "header.h"

    Test::Test() { qRegisterMetaType<Test>("Test"); }
    
    Custom::Custom() { qRegisterMetaType<Custom>("Custom"); }
    
    int main(int argc, char *argv[]) {
      QCoreApplication a(argc, argv);
      Test reg;
      return a.exec();
    }

when I compile the code I get below error.

/testing/build-test-Desktop_Qt_5_12_4_GCC_64bit-Debug/moc_CustomeTypes.cpp:175: error: no match for ‘operator!=’ (operand types are ‘Custom’ and ‘Custom’)
moc_CustomeTypes.cpp:175:31: error: no match for ‘operator!=’ (operand types are ‘Custom’ and ‘Custom’)
             if (_t->discovery != *reinterpret_cast< Custom*>(_v)) {
                 ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

is it mandatory to provide operator!= for Custom class?

Just to mention, if I change my Test class as below it compiles.

class Test {
  Q_GADGET
  Q_PROPERTY(QString ip MEMBER ip STORED true)
  Q_PROPERTY(Custom discovery READ discovery WRITE setDiscovery STORED true)

 public:
  Test();
  Custom discovery() const { return m_discovery; }
  void setDiscovery(const Custom& c) { m_discovery = c; }
  QString ip;
  Custom m_discovery;
};

Why Can't I use MEMBER for custom type?

1

There are 1 answers

0
spottedone On

Same issue with Qt 6.6, in moc generated code.

For me it helped to simply implement == and != operators, and everything went fine.