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

Malyushytsky, Alex alex at wai.com
Tue Jan 4 14:37:04 CET 2011


>> Isn't there such a way, because I have already a class which I want to use.

If you want your class the way it is, why do you ask for advice then?

All code you shown does - store additional data.
That brings an assumption that you make your QObject subclass just to be able to use qobject_cast.

If it is so, it is certainly not the best design.
Even if you need QObject subclass for other reasons, you should not use
qobject_cast the way you do, cause QGraphicsItem *pItem is NOT derived from QObject.

Qt supplies QGraphicsItem::type () and qgraphicsitem_cast(), to do such job.
In fact your QGraphicsRectItem derived class should override QGraphicsItem::type () as recommended by Qt documentation anyway.

At the same time according to my experience it is better to avoid subclassing existing
derived from QGraphicsItem Qt classes if possible.
The reason is that Qt uses type() itself in the different places and you would need to override more functions than you expect, otherwise item will behave differently than its base class.

For example in my version of Qt if selected QGraphicsRectItem subclass with overridden type() will be painted differently than QGraphicsRectItem. To fix mentioned problem you would have to override paint().

Such problems can be avoided if all you want is store additional data.
If you can't avoid it, follow Qt documentation recommendations when writing your QGraphicsItem derived classes.

In any case qobject_cast is a correct way of casting.
Hope this helps more.

Regards,
   Alex




-----Original Message-----
From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of NoRulez
Sent: Tuesday, January 04, 2011 2:15 AM
To: Qt Interest MailingList
Subject: Re: [Qt-interest] Best way to combine QGraphicsItem and qobject_cast

Isn't there such a way, because I have already a class which I want to use.

Best Regards
NoRulez

Am 04.01.2011 um 04:11 schrieb "Malyushytsky, Alex" <alex at wai.com>:

> Consider using setData:
>
> void QGraphicsItem::setData ( int key, const QVariant & value )
>
> Alex
>
> -----Original Message-----
> From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of NoRulez
> Sent: Monday, January 03, 2011 6:56 PM
> To: Qt Interest MailingList
> Subject: [Qt-interest] Best way to combine QGraphicsItem and qobject_cast
>
> 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
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>


---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”


More information about the Qt-interest-old mailing list