Results 1 to 6 of 6

Thread: identify QGraphicItem

  1. #1
    Join Date
    May 2006
    Posts
    57
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default identify QGraphicItem

    I have a class of QGraphicItem type that contains special properties and I wanted that when selected one of them, it knew as he is the one that they selected but not like QGraphicItem but like an object of the class which I did, somebody knows like doing this?
    Thanks

  2. #2
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: identify QGraphicItem

    how about:

    Qt Code:
    1. MyItem *item = NULL;
    2. item = qobject_cast<MyItem *>(selectedItem());
    3. if (item != NULL){
    4. item->doSomething();
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: identify QGraphicItem

    qobject_cast works only if the item also inherits QObject which the default QGraphicsItems don't inherit.
    So my advice is to use qgraphicsitem_cast instead (replace qobject_cast with qgraphics_cast in above example). Read this qt doc.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  4. #4
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: identify QGraphicItem

    Or just use standard C++ dynamic_cast<T>(). qobject_cast and qgraphicsitem_cast are only necessary for apps that are compiled with RTTI disabled; mostly for embedded apps, or for old compilers. For modern apps, I just use dynamic_cast ;-).

    Note: qgraphicsitem_cast does not support polymorphic casts.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  5. #5
    Join Date
    May 2006
    Posts
    57
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: identify QGraphicItem

    Well when it tries to use qgraphicsitem_cast I could not, then I wanted some tutorial or example to using it , Thanks.

    Sorry for my bad english.

  6. #6
    Join Date
    May 2006
    Posts
    57
    Thanks
    12
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: identify QGraphicItem

    Well... here the correct code for use qgraphicsitem_cast:

    Qt Code:
    1. Eitem = NULL;
    2. foreach (QGraphicsItem *item, Scene->selectedItems())
    3. {
    4. Eitem = qgraphicsitem_cast<MyClass*>(item);
    5. if (Eitem != NULL)
    6. {
    7. //My Code
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 


    Thanks for all

Similar Threads

  1. How to identify the process
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2007, 16:18
  2. Replies: 3
    Last Post: 11th March 2007, 13:04
  3. Location of QGraphicItem!!
    By rachana in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2007, 21:20
  4. need help with QGraphicItem
    By rachana in forum Qt Programming
    Replies: 13
    Last Post: 30th January 2007, 06:27
  5. How to identify event sender?
    By Caius Aérobus in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2006, 10:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.