如何在 Qt 中复制对象?

发布于 2024-08-28 23:56:52 字数 754 浏览 15 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-09-04 23:56:52

似乎复制构造函数和赋值运算符被禁用。来自

没有复制构造函数或赋值运算符

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.

以酷 2024-09-04 23:56:52

亚伦关于使用赋值运算符的说法是正确的。

我知道制作对象副本的唯一方法(如果确实需要的话)是使用 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.

断肠人 2024-09-04 23:56:52

如果您想传输数据并想使用最简单的方法,只需在两个小部件之间创建一个信号和槽即可。在主小部件中连接它们,并在调用插槽时传递数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文