首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >'QObject& QObject::operator=(const &)‘是私有的

'QObject& QObject::operator=(const &)‘是私有的
EN

Stack Overflow用户
提问于 2014-05-13 10:35:25
回答 1查看 4.9K关注 0票数 0

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

代码语言:javascript
复制
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;
                                       ^

部分wavingwidget.cpp

代码语言:javascript
复制
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;

部分石墨

代码语言:javascript
复制
class GraphicsPrimitive : public QObject { //graphicsprimitive.h:50:7

    Q_OBJECT
friend class PainterBar;

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

部分多边形

代码语言:javascript
复制
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的问题,但我不知道怎么回事。我也见过类似的问题,但没有发现任何可以帮助我的东西。

我会很高兴知道哪一部分失败了/寻找什么,这样我就可以更好地搜索解决方案编辑

我做了@棘轮怪告诉我的事,并设法解决了一些错误,但它仍然说它的私密:

代码语言:javascript
复制
..\..\..\..\..\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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-13 10:53:46

QObjects不是可复制的,只能由指针引用;因此,QList<SIZPolygon>的声明(正如我在您的错误消息中所看到的)迟早会导致问题。

如果不需要从QObject for GraphicsPrimitive中删除继承

或根据需要将其更改为QList<SIZPolygon*>和重新父级。

代码语言:javascript
复制
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()

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23628890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档