PDA

View Full Version : Propper way to remove a QGraphicsItem by calling a QAction of the QGraphicView



Cal
2nd December 2010, 14:43
Hey!

i have these two functions in my QGraphicView class:


void VideoWidget::contextMenuEvent(QContextMenuEvent * event) {
QGraphicsItem *item = itemAt(event->pos());

QMenu menu;
if (item&&item->flags().testFlag(QGraphicsItem::ItemIsSelectable))
menu.addAction(tr("Remove Item"),this, SLOT(deleteDoor(item)));
menu.addAction(tr("Add Door Selector"), this, SLOT(createDoor()));
menu.addAction(tr("Add Cage"), this, SLOT(markCage()));
menu.exec(event->globalPos());
}

void VideoWidget::deleteDoor(QGraphicsItem *item) {
scene.removeItem(item);
}


This contextMenu is called, if you click somewhere in the View. If there is an item at the view's mouse click position the action "remove item" is added to the context menu. The Problem is, i dont know how to connect the QAction properly to the deleteDoor(QGraphicItem*) slot, because i need to pass the items pointer as a parameter, but the action only emits triggered()

So What would you suppose is the best way to remove the item from the Scene by calling the context menu's action?

ness
2nd December 2010, 15:00
QAction has a data field you may use to transfer some data through the action call