我想将大量自定义对象与 Qt5 远程对象一起使用。正如我所读到的,它们可能不需要在 .REP 文件中创建为 POD。我试图做以下事情:
namespace my::api {
class Card {
Q_GADGET
Q_PROPERTY(QString type READ type WRITE setType)
[... removed property functions]
Q_PROPERTY(QString id READ id WRITE setId)
[... removed property functions]
public:
static void declareQML() {
qRegisterMetaType<my::api::Card>("Card");
}
};
inline QDataStream& operator<<(QDataStream& stream, const my::api::Card & value) {
stream << value.type() << value.id();
return stream;
}
inline QDataStream& operator>>(QDataStream& stream, quint::api::Card & value) {
QString tmp;
stream >> tmp;
value.setType(tmp);
stream >> tmp;
value.setId(tmp);
return stream;
}
class Result {
Q_GADGET
Q_PROPERTY(bool status READ status WRITE setStatus NOTIFY statusChanged)
public:
bool status() { return m_status; }
public slots:
void setStatus(bool p) { m_status = p; emit statusChanged(); }
private:
bool m_status;
public:
static void declareQML() {
qRegisterMetaType<my::api::Result>("Result");
}
Result() : m_code(false) {}
};
inline QDataStream& operator<<(QDataStream& stream, const my::api::Result & value) {
stream << value.status();
return stream;
}
inline QDataStream& operator>>(QDataStream& stream, my::api::Result & value) {
bool tmp;
stream >> tmp;
value.setStatus(tmp);
return stream;
}
}
然后在 .REP 中执行类似的操作:
SLOT(checkCard(my::api::Card card));
SIGNAL(checkCardResponse(my::api::Result result));
但是我得到
Trying to construct an instance of an invalid type, type id: 1073676288
Trying to construct an instance of an invalid type, type id: 655615
Trying to construct an instance of an invalid type, type id: 1073676288
来自客户端,当我尝试执行 checkCard() 时。
在启动应用程序后(在创建副本之前)立即调用所有 declareQML 内容。
任何人都可以向我发布一个完整/有效的示例,如何在 QT5 远程对象中使用自定义类而不在 REP 文件中将自定义类创建为 POD?