wayfaerer
8th March 2012, 01:08
I'm using a custom QGraphicsScene in which you can click to create a custom QGraphicsItem, then drag (with the mouse still held down) to resize it. To do this, overrode QGraphicsItem::mousePressEvent and mouseMoveEvent, and it worked fine. It was something like this:
MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
itemBeingCreated = new MyItem(event->pos()) // "itemBeingCreated" is declared in myscene.h
}
MyScene::mouseMoveEvent(QGraphicsMouseEvent *event)
{
itemBeingCreated->resize(/* etc */) // basically how far you drag it from its origin, that's how big it'll be
}
Now I am trying to add undo/redo capability with QUndoStack and QUndoCommand. So now my mousePressEvent does this
MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
undoStack->push(new AddNewItem(/* etc */)); // "AddNewItem" is a QUndoCommand
}
But I don't understand how I can access the item created by "AddNewItem" in order to resize it in MyScene::onMouseMove. Should I just implement a "QGraphicsItem *AddNewItem::getCreatedItem()", or will that "break" the command pattern and lead to problems?
MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
itemBeingCreated = new MyItem(event->pos()) // "itemBeingCreated" is declared in myscene.h
}
MyScene::mouseMoveEvent(QGraphicsMouseEvent *event)
{
itemBeingCreated->resize(/* etc */) // basically how far you drag it from its origin, that's how big it'll be
}
Now I am trying to add undo/redo capability with QUndoStack and QUndoCommand. So now my mousePressEvent does this
MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
undoStack->push(new AddNewItem(/* etc */)); // "AddNewItem" is a QUndoCommand
}
But I don't understand how I can access the item created by "AddNewItem" in order to resize it in MyScene::onMouseMove. Should I just implement a "QGraphicsItem *AddNewItem::getCreatedItem()", or will that "break" the command pattern and lead to problems?