PDA

View Full Version : QGraphicsScene - clone selected item by Alt+Move



qtsoul
3rd December 2011, 15:43
Hi!
I want clone selected item at QGraphicsScene if user start moving item with pressed Alt.
I wrote this code:


if(mouseEvent->modifiers() == Qt::AltModifier)
{
QList<QGraphicsItem *> itList = this->selectedItems();
if(itList.size()==1)
{
DiagramElement *de = (DiagramElement*)itList[0];
DiagramElement *declone = de->clone();

de->setSelected(false);

declone->setPos(mouseEvent->scenePos());
this->addItem(declone);

declone->setSelected(true);
}
QGraphicsScene::mouseMoveEvent(mouseEvent);
}


This code is in class-child of QGraphicsScene. DiagramElement is child of QGraphicsItem.
After cloning old and new items are selected and moving together.
Old item is to stay in place and not be selected.

Lykurg
4th December 2011, 09:53
Hi,

first just a note: avoid old c style casts. As to your problem (without testing), try to call QGraphicsScene::clearSelection() and then select the cloned item.

qtsoul
4th December 2011, 10:24
Thanks! I solved this problem wits methogs grabMouse() and ungrabMouse().