The code you gave is working for me, too. Maybe the reason is you are inheriting from QGraphicsItem but not QGraphicsRectItem.
Here is the code not working.
graphicsItem.h
#include <QtGui>
{
public:
RectItem
(qreal x, qreal y, qreal width, qreal height,
QGraphicsItem * parent
= 0);
int type() const;
};
#include <QtGui>
class RectItem : public QGraphicsRectItem
{
public:
RectItem(QGraphicsItem * parent = 0);
RectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent = 0);
enum {RectType = QGraphicsRectItem::UserType + 1};
int type() const;
};
To copy to clipboard, switch view to plain text mode
graphicsItem.cpp
#include "graphicsItem.h"
RectItem
::RectItem(qreal x, qreal y, qreal width, qreal height,
QGraphicsItem * parent
)
int RectItem::type() const {return RectType; }
#include "graphicsItem.h"
RectItem::RectItem(QGraphicsItem * parent)
: QGraphicsRectItem(parent){}
RectItem::RectItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem * parent)
: QGraphicsRectItem(x, y, width, height, parent){}
int RectItem::type() const {return RectType; }
To copy to clipboard, switch view to plain text mode
main.cpp
#include "graphicsItem.h"
int main()
{
RectItem rectItem;
if(qgraphicsitem_cast<RectItem*>(item) == NULL)
qDebug() << "qgraphicsitem_cast Failed";
else
qDebug() << "qgraphicsitem_cast Success";
if(dynamic_cast<RectItem*>(item) == NULL)
qDebug() << "dynamic_cast Failed";
else
qDebug() << "dynamic_cast Success";
return 0;
}
#include "graphicsItem.h"
int main()
{
RectItem rectItem;
QGraphicsItem * item = &rectItem;
if(qgraphicsitem_cast<RectItem*>(item) == NULL)
qDebug() << "qgraphicsitem_cast Failed";
else
qDebug() << "qgraphicsitem_cast Success";
if(dynamic_cast<RectItem*>(item) == NULL)
qDebug() << "dynamic_cast Failed";
else
qDebug() << "dynamic_cast Success";
return 0;
}
To copy to clipboard, switch view to plain text mode
Result:
qgraphicsitem_cast Failed
dynamic_cast Success
Bookmarks