如何在 Qt 中复制对象?
我正在使用 Qt,并且有一些真正的基本问题。我创建了自己的小部件 MyTest,它有一个变量 obj
。我需要从小部件外部的对象设置此变量 obj ,以便复制该变量而不仅仅是指向另一个对象的指针。我收到一条错误消息,但不知道如何执行这些基本操作。这是我正在使用的代码:
MyTest.h:
class MyTest : public QWidget
{
Q_OBJECT
public:
void setObj(QObject &inobj);
QObject obj;
....
}
MyTest.cpp:
void MyTest::setObj(QObject &inobj) {
obj = inobj; //HERE I get the error message: "illegal access from 'QObject' to protected/private member 'QObject::operator=(const QObject &)'"
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QObject *ob = new QObject();
MyTest w;
w.setObj(*ob);
}
I'm using Qt and have some real basic problems. I have created my own widget MyTest that have a variable obj
. I need to set this variable obj
from an object outside of the widget so that the variable is copied not just a pointer to another object. I get an error message and can't figure out how to do this basic stuff. This is the code I'm using:
MyTest.h:
class MyTest : public QWidget
{
Q_OBJECT
public:
void setObj(QObject &inobj);
QObject obj;
....
}
MyTest.cpp:
void MyTest::setObj(QObject &inobj) {
obj = inobj; //HERE I get the error message: "illegal access from 'QObject' to protected/private member 'QObject::operator=(const QObject &)'"
}
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QObject *ob = new QObject();
MyTest w;
w.setObj(*ob);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎复制构造函数和赋值运算符被禁用。来自此。
没有复制构造函数或赋值运算符
QObject 既没有复制构造函数也没有赋值运算符。这是设计使然。实际上,它们是通过宏 Q_DISABLE_COPY() 声明的,但在私有部分中。事实上,所有从 QObject 派生(直接或间接)的 Qt 类都使用此宏来声明其复制构造函数和赋值运算符为私有。推理可以在关于 Identity 的讨论中找到与 Qt 对象模型 页面上的 Value 相比。
主要结果是您应该使用指向 QObject(或 QObject 子类)的指针,否则您可能会想使用 QObject 子类作为值。例如,如果没有复制构造函数,则无法使用 QObject 的子类作为要存储在容器类之一中的值。您必须存储指针。
It seems the copy constructor and assignment operator are disabled. From this.
No copy constructor or assignment operator
QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.
The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.
亚伦关于使用赋值运算符的说法是正确的。
我知道制作对象副本的唯一方法(如果确实需要的话)是使用 QDataStream 类中描述的序列化。这将制作该对象的深层副本。
或者您是否考虑过将类包装为可以安全传递的 QSharedPointer 指针。但这将是对象的影子或参考副本。
Aaron is correct about using the assignment operator.
The only way that I'm aware of to make a copy of an object, if you really have to, is to use Serialization as described in QDataStream Class. This would make a deep copy of the object.
Or have you considered wrapping the class as a QSharedPointer pointer that you can safely pass around. This would be a shadow or reference copy of the object though.
如果您想传输数据并想使用最简单的方法,只需在两个小部件之间创建一个信号和槽即可。在主小部件中连接它们,并在调用插槽时传递数据。
if you want to transfer data and want to use the very simplest way, just create a signal and slot between both widgets. Connect them in the main widget, and pass data while calling the slot.