PDA

View Full Version : Find the last added Item in a QgraphicsScene



0backbone0
24th August 2015, 20:55
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'


scene->items().last()->setItemtitle("Test Node");

The setter and getter functions look like this. The String for the Itemtitle is Private.

QString getIiemtitle() const;
void setitemtitle(const QString &value);


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

Frdz
24th August 2015, 21:26
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. ^^

ChrisW67
24th August 2015, 21:29
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


SomeClass *item = new SomeClass();
item->setItemtitle("foo bar");
scene->addItem(item);

0backbone0
26th August 2015, 14:26
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.