微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Q_PROPERTY中使用自定义类型

如何解决在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 举报,一经查实,本站将立刻删除。