Search code examples
qtprivateqobject

QObject::QObject(QObject parent=0) is private in this context


I have a class called mesh. I want to keep track of its objects. So when ever a new mesh is created, I wanted to have a signal. And soo I've added

class mesh: public QObject

and made all methods as slots and added a signal to the c-tor meshCreated. But from here on my years of pain have started. No matter what, the error in the title started to appear. Even thought I commented all the lines i.e., added // before every line in the code, it still says the same. How can I fix this?

The actual code

#ifndef mesh_H
#define mesh_H

#include <QObject>
#include "mvert.h"
#include "medge.h"
#include "mface.h"

#include <QList>
#include <QString>
#include <QDebug>
#include "glmaterial.h"

class mesh : public QObject
{
    Q_OBJECT
public:
    explicit mesh(QObject *parent = 0);
    QString getName();

private:
    QString Name;
    GLMaterial material;

    QList<MVert> VertList;
    QList<MEdge> EdgeList;
    QList<MFace> FaceList;

    MVert Centroid;

    QList<int> QFaces;
    QList<int> TFaces;

public slots:
    void setName(QString Name);
    void ReadmeshData(QString meshsrc);
    void displayVerts();
    void displayEdges();
    void displayFaces();
    void addVert(MVert vert);
    void addEdge(MEdge edge);
    void addFace(MFace face);
    void removeDoubles();
    MVert generateCentroid();
    //FIXUS
    void sortQandT();
    void reorderFaces();
    void subDivFace(int index, int res);
    void forcedTriangulate();
    //FIXUS
    bool isVertInEdge(MVert input, MEdge edg);
    bool similarVerts(int i,int j);

    QList<int> relatedFacesToVert(MVert input);
    QList<int> relatedEdgesToVert(MVert input);
    QList<int> relatedVertsToVert(MVert input);
    QList<int> relatedFacesToEdge(MEdge input);
    QList<int> relatedEdgesToEdge(MEdge input);
    QList<int> relatedEdgesToFace(MFace input);
    QList<int> relatedFacesToFace(MFace input);

    QList<MVert> reducedVList();

    QList<MVert> vlistInstance();

    MVert midPointOfEdge(int i);
    MVert centroidOfFace(int i);

    mesh operator=(mesh input);

    void GLdraw();

signals:
    void meshCreated(mesh *mesh);

};

Solution

  • The line that is generating this error is this one:

    mesh operator=(mesh input);
    

    Classes which are derived from QObject are not allowed to have copy constructors. As the QObject documentation states:

    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.

    If you want to keep track of created mesh objects you can use for example QList<mesh*> from one of your other classes. Once the object is created you push it to the list etc.