Undo actions.
-
@Loc888
Hi
Please look at this
http://doc.qt.io/qt-5/qtwidgets-tools-undoframework-example.html
you can see how it uses the undoStack and how to use the system. -
Ye, i see that... And i am lost.
I read that, and i dont know wher i must declare any functionality....When i use the Stack.push(Command), how i am gonna tell to the "Command" to do something??
It's a variable, so how can i declare to it any functionality?
Add or subtract something? -
Hi
You let the commands points to the data so u can change it from inside them.
From sampleclass MoveCommand : public QUndoCommand { public: enum { Id = 1234 }; MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos, QUndoCommand *parent = 0); void undo() override; void redo() override; bool mergeWith(const QUndoCommand *command) override; int id() const override { return Id; } private: DiagramItem *myDiagramItem; // this would be your data. point to the variable QPointF myOldPos; // this would be old value }; and to restore you do void MoveCommand::undo() { myDiagramItem->setPos(myOldPos); // this would be to decrease or set old value back myDiagramItem->scene()->update(); setText(QObject::tr("Move %1") .arg(createCommandString(myDiagramItem, newPos))); }
-
Ther is no way to just undo a method or funtion? I have alot of this variables, so how many of those diagrams i am gonna create?
It's gonna mess up the code, at this point is a lot of stuff ther.Ps. It's not better, just to create one structure with all that data, and maybe then point to it?
-
Hi
how would u undo a method ?
Yes if u have a lots of single variables that need undo / redo then it will be many small classes.Since i dont know your use case or the code its hard to say if there would be easier way.
Do you need for each variable to be able to undo many levels or just last value ?
Also, you can make Commands class that can handle all, you just need to specify for what data member the undo applies when undoing etc.
But again, there are many ways to do this but it depends on the code u already have what will be easy.-Ps. It's not better, just tocreate one structure with all that data, and maybe then point to it?
Yes, using a struct would make is more easy to handle -
"Do you need for each variable to be able to undo many levels or just last value ?"
Ther almost everything must be able to undo, redo is not necessary. Needed 5 levels.
How to assign a structure member to that QUndoCommand object?
Ther is no way to point to the structure, because it's different type.
I dont find any method to set that equal. -
@Loc888
Hi
So each variable need to be able to undo many values back ?- How to assign a structure member to that QUndoCommand object?
You just do it. you can use any type u like.
class MoveCommand : public QUndoCommand { public: enum { Id = 1234 }; MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos, QUndoCommand *parent = 0);
the DiagramItem and QPointF could be ANYTHIng you need
MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
those are jsut what sample need, you can just use the types u need - How to assign a structure member to that QUndoCommand object?
-
@mrjj said in Undo actions.:
diagramItem
At this point, i am gonna create a structure, and create 10 objects, and then simply switch them, so:
Memory_001 = Memory_002
Memory_002 = Memory_003 // And progress.Is it a good idea? What about performance? (They are not so big, around 200 - 400 variables)
-
@Loc888
On a desktop class pc, it would be nothing for it.
But if u need multiple levels on undo such structure would not work.can you show some of the data u need multiple levels of undo for (real code) ?
are they int/floats, strings etc ?
Are they all of same type or very mixed? -
@mrjj Need at least5 of them, and max 10. Ther is rly no need to create more in this case.
I show you the template:
struct:Data
{int*
int*
int*long double*
long double*
long double*
long double*
long double*
long double*
long double*
long double*
long double*};
Stuff like that, i have around 4 - 6 of them, so it's like 30 objects * 10...
I think is much more then 300 variables, but not all must be able to undo,
cuz ther is a percentage calculator, so it's gonna re-calculate it again.
Ther are around +150 variables of "percent type", so they don't need to be stored, the function gonna recalculate them by current value of variables.So i am gonna create a structure with that six structures, and the total of variables gonna be maybe 800?? (Some long double) Gonna work?
-
I still think using the undo system ( if u need more than one level undo) is a better choice than make some sort of your own system.
-
@mrjj I don't remember, i work long time on this program, but i think nothing of long double need to be stored, ther is a structure with a template for percent calculations, so around 5 object's of them, does't need to be stores (Like i said, they gonna be re-calculated).
No, they are not pointers, i just put that " * " symbol, because i dont want write any names :)
In real it's:
struct:Data { // Names for the demonstration purposes int Name_A_Int; int Name_B_Int; int Name_C_Int; long double Name_A; long double Name_B; long double Name_C; long double Name_D; // And progress };
Ps. I am gonna try, will see what happend, i understood a little bit more, how it's gonna work, but i use it first time, and it's hard to use with a little bit bigger program.
-
@Loc888
Ahh next time use xxx as * screams pointer to us old ones :)
You could do something likeclass MyCommand : public QUndoCommand { public: enum { Id = 1234 }; MyCommand (struct_Data*data, long double * varToUse ,
so you give it the struct with data and the pointer to the member soi knows which of the values to alter.
Then when you push the command to the stack, you point the varToUse to the right one
inside struct. (else u need biiig if/switch case)You can make this very generic with templates if u know how to use such.
I have to go out now but ill be back tonight and can help get it running if still issues.
It looks more complicated than it is :)