将流运算符添加到 Qt 对象
Add stream operators to Qt object
是否可以将流运算符添加到现有的Qt对象中?我尝试在此示例代码中执行此操作(QFileSystemWatcher
),最后一行不编译。编译LineUser
示例。
用于注册流运算符的文档。
#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)
从这里:
具有公共默认构造函数(公共构造函数)的任何类或结构 可以注册复制构造函数和公共析构函数。
从这里开始:
QObject既没有复制构造函数,也没有赋值运算符。 这是设计使然。
因此,显然,存在一个设计限制,阻止QObject
派生注册为元类型(这也是有道理的,因为它不是必需的,因为默认情况下会为它们生成元信息),因此qRegisterMetaTypeStreamOperators
也是行不通的。
不过,它应该适用于QFileSystemWatcher *
。
相关文章:
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 如何注册Qt C++对象以在QML中使用它
- Qt对象所有权内存泄漏
- 以延长构造函数外部 QT 对象的生存期
- 我有一个 Qt 对象的 2d 数组,我已经用值播种了这些对象。如何访问数组中特定对象的值并更改它们?
- Qt对象如果使用'this'创建(更新)需要删除或自动删除?
- 在 cmake 中链接 Qt 对象
- 将流运算符添加到 Qt 对象
- 在 QT 对象类中声明 PCL 点云
- QT对象::连接:没有此类插槽信号到线程插槽
- 如何在调试期间查看Qt对象QByteArray的内容
- Qt对象删除导致崩溃
- 如何从线程的结果返回Qt对象(Qtfutur)
- QtWebkit javascript bridge,无法访问 Qt 对象方法
- 将 Qt 对象与 std::shared_ptr 一起使用
- 带有 Qt 对象的 Cmake 库
- 向QML公开复杂的Qt对象C++
- 从另一个 std::thread 调用 Qt 对象方法
- Qt 对象实例化查询
- C++中的静态成员Qt对象