是否可以将流运算符添加到现有Qt对象?我尝试在这个示例代码中执行此操作(对于QFileSystemWatcher
),最后一行不进行编译。 LineUser
示例已编译。
Documentation for registering stream operators
#include <QtCore>
class LineUser {
public:
int uId;
QString passwd;
qint8 statusType;
};
Q_DECLARE_METATYPE(LineUser)
QDataStream &operator<<(QDataStream &out, const LineUser &myObj) {
out<<myObj.uId<<myObj.passwd<<myObj.statusType;
return out;
}
QDataStream &operator>>(QDataStream &in, LineUser &myObj) {
in>>myObj.uId>>myObj.passwd>>myObj.statusType;
return in;
}
QDataStream &operator<<(QDataStream &out, const QFileSystemWatcher &myObj) {
return out;
}
QDataStream &operator>>(QDataStream &in, QFileSystemWatcher &myObj) {
return in;
}
int main() {
qRegisterMetaTypeStreamOperators<LineUser>("LineUser");
qRegisterMetaTypeStreamOperators<QFileSystemWatcher>("QFileSystemWatcher");
}
编译器消息很长,这里有一些错误:
error: use of deleted function ‘QFileSystemWatcher::QFileSystemWatcher(const QFileSystemWatcher&)’
return new (where) T(*static_cast<const T*>(t));
note: ‘QFileSystemWatcher::QFileSystemWatcher(const QFileSystemWatcher&)’ is implicitly deleted because the default definition would be ill-formed:
class Q_CORE_EXPORT QFileSystemWatcher : public QObject error: ‘QObject::QObject(const QObject&)’ is private
Q_DISABLE_COPY(QObject)
error: within this context class Q_CORE_EXPORT QFileSystemWatcher : public QObject note: declared here
Q_DISABLE_COPY(QObject)