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:
{
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
}
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
}
To copy to clipboard, switch view to plain text mode
Now I am trying to add undo/redo capability with QUndoStack and QUndoCommand. So now my mousePressEvent does this
{
undoStack->push(new AddNewItem(/* etc */)); // "AddNewItem" is a QUndoCommand
}
MyScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
undoStack->push(new AddNewItem(/* etc */)); // "AddNewItem" is a QUndoCommand
}
To copy to clipboard, switch view to plain text mode
But I don't understand how I can access the item created by "AddNewItem" in order to resize it in MyScene:
nMouseMove. Should I just implement a "QGraphicsItem *AddNewItem::getCreatedItem()", or will that "break" the command pattern and lead to problems?
Bookmarks