[Qt-interest] Best way to combine QGraphicsItem and qobject_cast

NoRulez norulez at me.com
Tue Jan 4 03:55:42 CET 2011


Hi,

I've several QGraphicsItems such as QGraphicsRectItem.
Now I want to have a setId() and a getId() function for each item.
So, what is the best way to do so?

As the result I want to do something like

foreach (QGraphicsItem *pItem, pScene->selectedItems()) {
    qDebug() << qobject_cast<MyBaseClass*>(pItem)->getId();
}

My idea was to add a class and inherit from it:

class MyBaseClass : public QObject {
    Q_OBJECT
public:
    void setId(const QString&);
    QString getId() const;
};

class MyItem : public QGraphicsRectItem, public MyBaseClass {
};

Thanks in advance

Best Regards
NoRulez



More information about the Qt-interest-old mailing list