PDA

View Full Version : QGraphicsItem subclass compilation error



^NyAw^
30th January 2013, 11:30
Hi,

I have a class that inherit from QGrpahicsEllipseItem to let it emit a signal when it is selected



#ifndef QDEFECTITEM_H
#define QDEFECTITEM_H

#include <QGraphicsEllipseItem>

class QDefectItem : public QGraphicsEllipseItem
{
Q_OBJECT

public:
QDefectItem();
~QDefectItem();

protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);

signals:
void itemSelected(QGraphicsItem*);
};

#endif // QDEFECTITEM_H




#include "QDefectItem.h"

QDefectItem::QDefectItem()
{

}

QDefectItem::~QDefectItem()
{

}

QVariant QDefectItem::itemChange(GraphicsItemChange change,const QVariant &value)
{
if (change == QGraphicsItem::ItemSelectedChange)
emit itemSelected(this);
return value;
}


This simple code returns an error when tryies to compile the generated moc file.
Missing something?

Thanks,

Santosh Reddy
30th January 2013, 11:37
To make use of signal/slots the class should be derived from directly or indirectly from QObject. QGraphicsEllipseItem does not have QObject in it's inheritnace tree. One thing you can do is, multiple inheritance of QObject and QGraphicsEllipseItem


class QDefectItem : public QObject, public QGraphicsEllipseItem
{
Q_OBJECT
...
};

^NyAw^
30th January 2013, 12:12
Hi,

I was lloking the "diagramscene" example and didn't notice that QGraphicsTextItem inherits from QGraphicsObject that is needed to use signal/slots.

Thanks,

Added after 27 minutes:

Now, I get "cannot create instance from an absract class"

Santosh Reddy
30th January 2013, 12:32
Now, I get "cannot create instance from an absract class"
What abstract class you are referring to?

^NyAw^
30th January 2013, 12:40
Hi,

Just when I call this:


QDefectItem *item = new QDefectItem();


I also get an error when calling "isEnabled()"


#ifndef QDEFECTITEM_H
#define QDEFECTITEM_H

#include <QGraphicsEllipseItem>

class QDefectItem : public QGraphicsObject, public QGraphicsEllipseItem
{
Q_OBJECT

public:
QDefectItem();
~QDefectItem();

private slots:
void enabledChg();

signals:
void itemSelected(QDefectItem*);
};

#endif // QDEFECTITEM_H




#include "QDefectItem.h"

QDefectItem::QDefectItem()
{
//connect(this,SIGNAL(enabledChanged()),this,SLOT(en abledChg()));
}

QDefectItem::~QDefectItem()
{

}

void QDefectItem::enabledChg()
{
//here there is another error
if (isEnabled()) //error C2358:"ambiguous acces to isEnabled()" and error C3861:"isEnabled identifier not found"
emit itemSelected(this);
}

wysota
30th January 2013, 12:48
Inherit from QObject instead of QGraphicsObject. The latter is an abstract class anyway.

^NyAw^
30th January 2013, 13:07
Hi,

And now, how can I get a signal when the item is selected if it not inherit from QGraphicsObject?
Tried to use "itemChange" but is not called when I click an item.

Thanks,

wysota
30th January 2013, 13:09
And now, how can I get a signal when the item is selected if it not inherit from QGraphicsObject?
Exactly the same way you'd do it if it inherited from QGraphicsObject since QGraphicsObject doesn't provide any means to be notified when an item gets selected.

^NyAw^
30th January 2013, 13:24
Hi,

Just adding "setFlag(QGraphicsItem::ItemIsSelectable)" to the items.

Thanks,

Santosh Reddy
30th January 2013, 15:07
Did you try this code from your first post?


QVariant QDefectItem::itemChange(GraphicsItemChange change,const QVariant &value)
{
if (change == QGraphicsItem::ItemSelectedChange)
emit itemSelected(this);
return value;
}

^NyAw^
30th January 2013, 15:55
Hi,

Yes, but I forget to set the item selectable.

Thanks,