“的QObject&安培; QObject :: operator =(const QObject&)'是私有的

时间:2014-05-13 10:35:26

标签: c++ qt migration copy-constructor

将项目从Qt4迁移到Qt5我得到了这个错误,我已经研究过,显然你不能从QObject创建一个派生类的复制构造函数(这是不可思议的,因为这段代码不是我的,它应该在以前的版本中编译)。复制构造函数并不真正复制任何QObject值,我不知道QList的东西是不是错了。

In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtGui/qwindowdefs.h:45:0,
                 from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:45,
                 from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
                 from ..\marssies\wavingwidget.h:4,
                 from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = SIZArrow]':
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:515:32:   required from 'void QList<T>::append(const T&) [with T = SIZArrow]'
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:301:49:   required from 'void QList<T>::push_back(const T&) [with T = SIZArrow]'
..\marssies\wavingwidget.cpp:345:23:   required from here
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtGui/qwindowdefs.h:45:0,
                     from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:45,
                     from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
                     from ..\marssies\wavingwidget.h:4,
                     from ..\marssies\wavingwidget.cpp:1:
    ..\marssies\graphicsprimitive.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = SIZPolygon]':
    ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:515:32:   required from 'void QList<T>::append(const T&) [with T = SIZPolygon]'
    ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:301:49:   required from 'void QList<T>::push_back(const T&) [with T = SIZPolygon]'
    ..\marssies\wavingwidget.cpp:330:24:   required from here
    ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qglobal.h:981:12: error: 'QObject& QObject::operator=(const QObject&)' is private
         Class &operator=(const Class &) Q_DECL_EQ_DELETE;
                ^
    ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:465:5: note: in expansion of macro 'Q_DISABLE_COPY'
         Q_DISABLE_COPY(QObject)
         ^
    In file included from ..\marssies\wavingwidget.h:10:0,
                     from ..\marssies\wavingwidget.cpp:1:
    ..\marssies\graphicsprimitive.h:50:7: error: within this context
     class GraphicsPrimitive : public QObject {
           ^
    In file included from ..\marssies\wavingwidget.h:13:0,
                     from ..\marssies\wavingwidget.cpp:1:
    ..\marssies\polygon.h:6:7: note: synthesized method 'GraphicsPrimitive& GraphicsPrimitive::operator=(const GraphicsPrimitive&)' first required here 
     class SIZPolygon : public GraphicsPrimitive {
           ^
    In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:51:0,
                     from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:46,
                     from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
                     from ..\marssies\wavingwidget.h:4,
                     from ..\marssies\wavingwidget.cpp:1:
    ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:372:35: note: synthesized method 'SIZPolygon& SIZPolygon::operator=(const SIZPolygon&)' first required here 
         else *reinterpret_cast<T*>(n) = t;
                                       ^

waveswidget.cpp的一部分

SIZPolygon d;
/* ...more code...*/
d.setData(TRIANGLE_VERTEX_FIRST, w1Data.pointNumber);
d.setData(TRIANGLE_VERTEX_SECOND, w2Data.pointNumber);
d.setData(TRIANGLE_VERTEX_THIRD, w3Data.pointNumber);
d.setData(POLYGON_WAVEHEIGHT, waveAltVal);

polygons.push_back(d); //wavingwidget.cpp:330:24

double waveComputedDir = (w1Data.waveDirection + w2Data.waveDirection + w3Data.waveDirection) / 3.0;

graphicsprimitive.h的一部分

class GraphicsPrimitive : public QObject { //graphicsprimitive.h:50:7

    Q_OBJECT
friend class PainterBar;

public:
    GraphicsPrimitive(void);
    GraphicsPrimitive(const GraphicsPrimitive&);
    ~GraphicsPrimitive(void);

polygon.h的一部分

class SIZPolygon : public GraphicsPrimitive { //polygon.h:6:7

public:
    SIZPolygon();
    ~SIZPolygon(void);
    void draw(QPainter*);
    void addPoint(double, double, int);
    bool containsScreen(int, int);
    bool containsGeo(double, double);
    QList<QPair<int, int> > getCenterPoints();
    QPair<double, double> getGeoCenterPoint();
    bool isOverNode(int, int, int*);
    void movePoint(int, int, int);
    void move(int, int);
    void toXML(QDomElement*, QDomDocument*);
    int containsSegment(int, int);

private:
    void countCOFMA(double*, double, double, double, double);
};

正如你所看到的那样,我通过常量引用传递QObject,所以实际上我不确定代码是否有问题,它可能是QList的东西,但我不知道如何。我已经看到了类似的问题,但没有找到任何可以帮助我的东西。
我会很高兴知道哪个部分失败/寻找什么,这样我就可以更好地寻找解决方案 的修改

我做了@ratchetfreak告诉我的事情,设法解决了一些错误,但仍然说它是私有的:

..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qglobal.h:981:12: error: 'QObject& QObject::operator=(const QObject&)' is private
     Class &operator=(const Class &) Q_DECL_EQ_DELETE;
            ^
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:465:5: note: in expansion of macro 'Q_DISABLE_COPY'
     Q_DISABLE_COPY(QObject)
     ^
In file included from ..\marssies\wavingwidget.h:10:0,
                 from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h:50:7: error: within this context
 class GraphicsPrimitive : public QObject {
       ^
In file included from ..\marssies\wavingwidget.h:11:0,
                 from ..\marssies\wavingwidget.cpp:1:
..\marssies\arrow.h:7:7: note: synthesized method 'GraphicsPrimitive& GraphicsPrimitive::operator=(const GraphicsPrimitive&)' first required here 
 class SIZArrow : public GraphicsPrimitive
       ^
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qobject.h:51:0,
                 from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:46,
                 from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
                 from ..\marssies\wavingwidget.h:4,
                 from ..\marssies\wavingwidget.cpp:1:
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:372:35: note: synthesized method 'SIZArrow& SIZArrow::operator=(const SIZArrow&)' first required here 
     else *reinterpret_cast<T*>(n) = t;

1 个答案:

答案 0 :(得分:7)

QObjects不可复制,只能由指针引用;所以QList<SIZPolygon>的声明(正如我在你的错误信息中看到的那样)迟早会引起问题。

如果不是真的需要,可以从QObject for GraphicsPrimitive中删除继承

或将其更改为QList<SIZPolygon*>并根据需要重新显示

SIZPolygon* d = new SIZPolygon();
d->setParent(this);
//convention is to add a QObject parent=0 to the constructor but this works as well

当您需要删除它时,请在其上调用d->deleteLater()