Home > Software design >  Why am I getting moc errors for QGraphicsPolygonItem reimplementation
Why am I getting moc errors for QGraphicsPolygonItem reimplementation

Time:09-26

Please help me! I'm on a small personal project that I needed some sort of node editor for. And for that I have to define the Individual class which inherits from the QGraphicsPolygonItem class. I simplified the code as much as possible to be able to identify the error but to no avail. I'm getting MOC (Meta Object Compiler) errors which I honestly don't understand (we're all newbies at some level). Also, there is nothing about it on the web and you are my last resort.

Here is the Individual class definition:

#ifndef INDIVIDUAL_H
#define INDIVIDUAL_H

#include <QGraphicsPolygonItem>
#include <QGraphicsTextItem>
#include <QPainterPath>

class Individual : public QGraphicsPolygonItem
{
    Q_OBJECT
public:
    Individual(QString p_fName, QString p_lName,QGraphicsItem* parent = 0);

};

#endif // INDIVIDUAL_H

And here's its implementation:

#include "individual.h"

Individual::Individual(QString p_fName , QString p_lNamen, QGraphicsItem* parent)
    :QGraphicsPolygonItem(parent)

{

    QPainterPath temp_path;
    temp_path.addRoundRect(0,0,100,50,10,10);
    setPolygon(temp_path.toFillPolygon());
}

And then on the main.cpp (to be simple) i wrote something like this:

#include "widget.h"
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <individual.h>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.setMinimumSize(800,600);
    
    QGraphicsView view(&w);
    view.setMinimumSize(800,600);
    
    QGraphicsScene scene;
    Individual myPoly("My","Name"); // The arguments are not used for now just kept them there in case
    scene.addItem(&myPoly);

    view.setScene(&scene);

    w.show();
    return a.exec();
}

And finally the errors i get evrytime:

enter image description here

CodePudding user response:

Because QGraphicsPolygonItem is a QGraphicsItem and it's not a QObject. You can use multiple inheritance feature of C , to make the Individual class both of them at the same time by

 Individual : public QObject, public QGraphicsPolygonItem
{
...

and don't forget to call a QObject ctor for Individual ctore.

Now, I should recommend you to read this question and the answers as well.

  • Related