Strange casting error for qgraphicsview?
-
I have an object that inherits from QGraphicsitem, and it has an enum, when I write that code, it gives error for enum casting problem. Why that happens?
The problem is here :
@
Node *startItem =
qgraphicsitem_cast<Node *>(startItems.first());
Node *endItem =
qgraphicsitem_cast<Node *>(endItems.first());
@
@
if (mLink != 0 && mMode == Insert_Line) {
QList<QGraphicsItem *> startItems = items(mLink->line().p1());
if (startItems.count() && startItems.first() == mLink)
startItems.removeFirst();
QList<QGraphicsItem *> endItems = items(mLink->line().p2());
if (endItems.count() && endItems.first() == mLink)
endItems.removeFirst();removeItem(mLink); delete mLink; // if two nodes are connected by the line if (startItems.count() > 0 && endItems.count() > 0 && startItems.first()&& endItems.first() && startItems.first() != endItems.first()) { Node *startItem = qgraphicsitem_cast<Node *>(startItems.first()); Node *endItem = qgraphicsitem_cast<Node *>(endItems.first()); // add a line Link *arrow = new Link(startItem,endItem); arrow->trackNodes(); addItem(arrow); arrow->updatePosition(); }
@
-
Thanks for reply.
Windows7, QTCreator, Mingw.
The error is
casting error <Node*>
and it is in that line:
Node *startItem =
qgraphicsitem_cast<Node *>(startItems.first());
Node *endItem =
qgraphicsitem_cast<Node *>(endItems.first());my node class is :
@
class Node : public QGraphicsItem
{
Q_DECLARE_TR_FUNCTIONS(Node)
public:
enum Type { Input, Output};public:
Node(QGraphicsItem *parent = 0, const Type& type = Input); ~Node(); void setText(const QString &text);
};
@ -
quote from docs:
bq. To make this function work correctly with custom items, reimplement the "type()":http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#type function for each custom QGraphicsItem subclass.
-
I just need a user defined enumeration, why should I reimplement type function?
-
@template <class T> inline T qgraphicsitem_cast(QGraphicsItem *item)
{
return int(static_cast<T>(0)->Type) == int(QGraphicsItem::Type)
|| (item && int(static_cast<T>(0)->Type) == item->type()) ? static_cast<T>(item) : 0;
}
@As you see item->type() is used in qgraphicsitem_cast function