gpt4 book ai didi

c++ - 向 Qt 对象添加流运算符

转载 作者:行者123 更新时间:2023-11-28 05:22:23 25 4
gpt4 key购买 nike

是否可以将流运算符添加到现有的 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)

最佳答案

来自 here :

Any class or struct that has a public default constructor, a public copy constructor and a public destructor can be registered.

来自here :

QObject has neither a copy constructor nor an assignment operator. This is by design.

显然,有一个设计限制阻止 QObject derived 被注册为元类型(这也是有意义的,因为它不是必需的,因为默认情况下会为它们生成元信息),因此 qRegisterMetaTypeStreamOperators 也是不行的。

虽然它应该适用于 QFileSystemWatcher *

关于c++ - 向 Qt 对象添加流运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com