QGraphicsItem child's type()
-
Hello
I've become a little bit confused during development of application, which uses QGraphicsView framework.
As you know, QGraphicsItem has type() method returning type index of type of an object. In examples and documentations you may found something like this:
@
class CustomItem : public QGraphicsItem {
...
enum { Type = UserType + 1 };int type() const { // Enable the use of qgraphicsitem_cast with this item. return Type; } ...
};
@
The problem I see is that if you have lots of custom graphics items, you have to remember numbers that are already used in order to keep type indexes valid. And you might have problems if you will use items, created by someone else.
One possible solution I see is using something like the following code:
@
class CustomItem : public QGraphicsItem {
...
int type() const {
// Enable the use of qgraphicsitem_cast with this item.
static int _type = ( ( (_type = (int) (&_type) ) > UserType )
? _type
: (_type + UserType ) );
return _type;
}
...
};
@
To make this work you should use this implementation in each derived class.
Seems like it will work, but it's ugly and smells.
So my questions are:- is type() a real problem or I envented it myself?
- if so, is my solution good enough?
- if not, what is the right solution?
-
Unfortunately, I've found that my hack won't work with Qt now, but the question about quality of this solution and about other possible solutions is still actual.