0

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?

Vencat
  • 1,272
  • 11
  • 36
  • I assume the complained line is part of a moc-written code which sets the property only if the current value is distinct from the new. I found a similar issue in the Qt forum: [Best way to access a cpp structure in QML](https://forum.qt.io/topic/82325/best-way-to-access-a-cpp-structure-in-qml/13) – Scheff's Cat Oct 23 '20 at 21:46
  • 3
    It's odd to me that you call qRegisterMetaType() from the gadget's constructor. I normally do that before I try to use the class. Maybe that's ok, but I'm not sure. At the very least, it will re-register the type every time you create a new instance. – JarMan Oct 24 '20 at 03:59

0 Answers0