Results 1 to 4 of 4

Thread: Find the last added Item in a QgraphicsScene

  1. #1
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Find the last added Item in a QgraphicsScene

    Hi,

    I am trying to access the last Item that was added to a scene. (custom Item derived from QGraphicsItem)
    Setitemtitle is a member Function that takes a QString as Argument. Is there a easy way of using it?

    I tried to do it this way, but just get an error: 'class QGraphicsItem' has no member named 'setItemtitle'

    Qt Code:
    1. scene->items().last()->setItemtitle("Test Node");
    To copy to clipboard, switch view to plain text mode 

    The setter and getter functions look like this. The String for the Itemtitle is Private.
    Qt Code:
    1. QString getIiemtitle() const;
    2. void setitemtitle(const QString &value);
    To copy to clipboard, switch view to plain text mode 

    Or is it better to store pointers to the Items in a List or Vector?

  2. #2
    Join Date
    Aug 2015
    Location
    Gdansk, Poland
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Find the last added Item in a QgraphicsScene

    Quote Originally Posted by 0backbone0 View Post
    Or is it better to store pointers to the Items in a List or Vector?
    Previously I used those method to store my last history in 2 length of array (current & last), or using push & pull to stack.
    I'm also waiting if there's any better approach from any masters here. ^^

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Find the last added Item in a QgraphicsScene

    QGraphicsScene::items() returns a list of pointers to QGraphicsItems, not a list of pointers to some custom class. As the error message says, QGraphicsItem has no setItemtitle() function.

    If a particular QGraphicsItem is of a class derived from QGraphicsItem then you can use qgraphicsitem_cast<SomeClass *>() to get a pointer of the right type.

    It is not clear why you want the last item added. If you want to set the title on an item you are in the process of adding then do it before you put it in the scene
    Qt Code:
    1. SomeClass *item = new SomeClass();
    2. item->setItemtitle("foo bar");
    3. scene->addItem(item);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Find the last added Item in a QgraphicsScene

    The setItemtitle was just an example (probably a poor one).
    The point is that the user has a bunch of buttons, one to add an item, and the others will do something to this item.
    After some experimenting I found that it is probably simplest to just use a pointer to store the last added item in for this.

Similar Threads

  1. Replies: 5
    Last Post: 10th March 2014, 05:40
  2. Replies: 1
    Last Post: 5th March 2012, 19:26
  3. Replies: 1
    Last Post: 28th October 2010, 22:36
  4. Replies: 0
    Last Post: 27th July 2010, 12:48
  5. Replies: 4
    Last Post: 30th October 2009, 07:36

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.