Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsItem child's type()
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItem child's type()

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wilk
    wrote on last edited by
    #1

    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:

    1. is type() a real problem or I envented it myself?
    2. if so, is my solution good enough?
    3. if not, what is the right solution?
    1 Reply Last reply
    0
    • W Offline
      W Offline
      Wilk
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved