PDA

View Full Version : QGraphicsItemPrivate and qgraphicsitem_cast



ttvo
3rd September 2009, 00:01
Hi,

I have a QGraphicsTextItem custom object that contains QGraphicsRectItem(s). In my scene's mouseReleaseEvent, I have code to look through the QList<QGraphicsItem *> and only act when the item is of a specific type, Port::Type which is enum { Type = UserType + 16 };

I notice that my QList<QGraphicsItem *> also contains the QGraphicsItemPrivate d_ptr when I select on the QGraphicsTextItem custom object itself, resulting in a crash on the "int aType = anItem->type();" line

My questions are:
1) why the d_ptr is returned in my items() call
2) how am I going to modify my following code to just look for a specific type, Port *, but ignore everything else in the items() call. Thanks



Port *oport = 0;
while (!startItems.isEmpty()) {
QGraphicsItem* anItem = startItems.takeFirst();
if (anItem) {
int aType = anItem->type();
if (aType == Port::Type) {
oport = qgraphicsitem_cast<Port *>(anItem);
break;
}
}
}

where Port is a QGraphicsRectItem

wysota
3rd September 2009, 08:13
QList<QGraphicsItem*> allItems = scene->items();
QList<Port*> portItems;
foreach(QGraphicsItem *item, allItems){
Port *portItem = qgraphicsitem_cast<Port*>(item);
if(portItem) portItems << portItem;
}

Where:

class Port : public QGraphics...Item {
public:
enum { Type = UserType+16 };
int type() const { return Type; }
};