qgraphicsitem_cast cannot convert seft-defined types
I have a class inherits QGraphicsRectItem with type function
Code:
int RectItem::type() const
{
return UserType + 1;
}
But when I try to coverting a QGraphicsItem pointer to RectItem pointer, the qgraphicsitem_cast returns null.
Code:
QList<QGraphicsItem *> items = scene()->selectedItems();
for(int i=0; i<items.size(); i++){
if(items[i]->type() == UserType + 1)
RectItem*rectItem = qgraphicsitem_cast<RectItem*>(items[i]);
Here rectItem is null. However if I change the cast to dynamic_cast, the convertion will be successful.
Code:
QList<QGraphicsItem *> items = scene()->selectedItems();
for(int i=0; i<items.size(); i++){
if(items[i]->type() == UserType + 1){
RectItem*rectItem = dynamic_cast<RectItem*>(items[i]);
Here rectItem is the real RectItem selected.
Anyone knows why qgraphicsitem_cast is not working?
Thanks in advance.
Re: qgraphicsitem_cast cannot convert seft-defined types
Implement it this way
Code:
{
...
public:
...
enum { Type = UserType + 1 };
int type() const
{
return Type;
}
...
};
Re: qgraphicsitem_cast cannot convert seft-defined types
I tried to use class enum and global enum, but they work the same as without using enum—— type checking returns true, but qgraphicsitem_cast returns null.
Re: qgraphicsitem_cast cannot convert seft-defined types
Here is an example, it works perfectly, if it still does not work for you, please post a minimum compilable code
Code:
#include <QtGui>
{
public:
enum { Type = UserType + 1 };
int type() const { return Type; }
};
int main(int /*argc*/, char **/*argv*/)
{
RectItem rectItem;
qDebug() << "1." << &rectItem;
qDebug() << "2." << qgraphicsitem_cast<RectItem*>(item);
return 0;
}
Output:
1. QGraphicsItem (this = 0x22feec , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags = ( ) )
2. QGraphicsItem (this = 0x22feec , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags = ( ) )
Re: qgraphicsitem_cast cannot convert seft-defined types
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
Code:
#include <QtGui>
{
public:
RectItem
(qreal x, qreal y, qreal width, qreal height,
QGraphicsItem * parent
= 0);
int type() const;
};
graphicsItem.cpp
Code:
#include "graphicsItem.h"
RectItem
::RectItem(qreal x, qreal y, qreal width, qreal height,
QGraphicsItem * parent
)
int RectItem::type() const {return RectType; }
main.cpp
Code:
#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;
}
Result:
qgraphicsitem_cast Failed
dynamic_cast Success
Re: qgraphicsitem_cast cannot convert seft-defined types
Ahh, I see your problem, In QGraphicsRectItem class type() function is no more virtual, so inherting from QGraphicsRectItem, and re-implementing type() function is a mear function override in derived class, and is no more polymorphic (virtual) function.
There are two ways to solve, see which one will fit your requirement.
1. Inherit directy from QGraphicsItem, and reimplement type(), and you know what to do then.
2. Inherit from QGraphicsRectItem, and don't implement type(), and offcourse you can use only dynamic_cast<> (qgraphicsitem_cast will not work )
Re: qgraphicsitem_cast cannot convert seft-defined types
Thanks a lot. Actually I think I can still implement type() to override the type() function of QGraphicsRectItem. This may help if I have multiple children classes like RectItem1, RectItem2, etc.
Re: qgraphicsitem_cast cannot convert seft-defined types
Quote:
Originally Posted by Santosh Reddy
Ahh, I see your problem, In QGraphicsRectItem class type() function is no more virtual, so inherting from QGraphicsRectItem, and re-implementing type() function is a mear function override in derived class, and is no more polymorphic (virtual) function.
This is not correct, I am sorry for the inconvenience caused.
The real reason your code is not wokring is that the enum name is supposed to be "Type" (not "RectType"). Modify you code like this
Code:
#include <QtGui>
{
public:
RectItem
(qreal x, qreal y, qreal width, qreal height,
QGraphicsItem * parent
= 0);
//enum {RectType = QGraphicsRectItem::UserType + 1};
int type() const;
};
RectItem
::RectItem(qreal x, qreal y, qreal width, qreal height,
QGraphicsItem * parent
)
//int RectItem::type() const {return RectType; }
int RectItem::type() const {return Type; }