当创建从qt中的qgraphicspixmapitem创建类中时,尝试引用已删除的函数错误

attempting to reference a deleted function error when create a class inheriting from QGraphicsPixmapItem in QT

本文关键字:创建 引用 删除 错误 函数 中的 qt qgraphicspixmapitem      更新时间:2023-10-16

我想在我的场景中插入图片,并在检测到图片位置更改时做某事。为了实现这一点,我创建了一个从qgraphicspixmapitem继承的类背景pic。但是,它出现了C2280错误:" BackgroundPic :: BackgroundPic(const BackgroundPic&)':尝试在我尝试编译时尝试引用已删除的函数(编译源文件mainwindow.cpp)。有人知道我的代码中有什么问题?

#include <qstring.h>
#include <qgraphicsproxywidget.h>

class BackgroundPic: public QGraphicsPixmapItem
{
public:
    BackgroundPic()
    {
        show_flag = true;
        moveable_flag = true;
    }
    //QString name;
    float x_original;       //physical
    float y_original;
    float x;        //physical
    float y;
    QString pic_address;
    bool show_flag;
    bool moveable_flag;
    QVariant itemChange(GraphicsItemChange change, const QVariant &value)
    {
        if (change == ItemPositionChange)
        {
            x = x_original + value.toPointF().x();
            y = y_original + value.toPointF().y();
        }
        return QGraphicsItem::itemChange(change, value);
    }
};

问题似乎很清楚,并且在您发布的代码中都不是。在文件mainwindow.cpp中,您可以调用复制构造函数(this BackgroundPic :: backgroundPic(const BackgroundPic&amp;)),但是此函数不可用。

普通复制构造函数是自动定义的(您可以阅读有关零规则和五个规则的规则),但是在您的情况下,这不是。之所以发生,是因为您从没有复制构造函数的类中继承。这意味着您的班级也不可复制。