在Q_PROPERTY中使用自定义类型

问题描述

我无法在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();
    }

当我编译代码时,我得到下面的错误。

/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)) {
                 ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

是否必须为自定义类提供 operator!=

仅提及一下,如果我将下面的Test类更改为可编译的。

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;
};

为什么不能使用成员作为自定义类型?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...