QLineEdit undo/redo does not work from my edit menu
-
I am working with Qt5 and have implemented an undo stack for my application.
undoStack = new QUndoStack (); undoStack->setUndoLimit (100);
I build my undo/redo menu items like this:
QAction *undoAction = undoStack->createUndoAction(this, tr("&Undo")); undoAction->setShortcuts(QKeySequence::Undo); QAction *redoAction = undoStack->createRedoAction(this, tr("&Redo")); redoAction->setShortcuts(QKeySequence::Redo); ui->menuEdit->addAction(undoAction); ui->menuEdit->addAction(redoAction);
All of which works fine when I push my commands onto the undo stack. However when a user types into a QLineEdit the insert commands don't know about my undoStack. I assume there is some internal stack, but I don't know how to access it.
The result is that the user can type ctrl-Z in the QLineEdit and it will undo, but if they try to use the undo menu item, it knows nothing about the recent edits and undoes some earlier application action.I looked at the code for QLineEdit, but it is using internal objects which I could not even find:
void QLineEdit::undo() { Q_D(QLineEdit); d->resetInputMethod(); d->control->undo(); }
Is there a way to tell the QLineEdit to use my undoStack?
-
I am working with Qt5 and have implemented an undo stack for my application.
undoStack = new QUndoStack (); undoStack->setUndoLimit (100);
I build my undo/redo menu items like this:
QAction *undoAction = undoStack->createUndoAction(this, tr("&Undo")); undoAction->setShortcuts(QKeySequence::Undo); QAction *redoAction = undoStack->createRedoAction(this, tr("&Redo")); redoAction->setShortcuts(QKeySequence::Redo); ui->menuEdit->addAction(undoAction); ui->menuEdit->addAction(redoAction);
All of which works fine when I push my commands onto the undo stack. However when a user types into a QLineEdit the insert commands don't know about my undoStack. I assume there is some internal stack, but I don't know how to access it.
The result is that the user can type ctrl-Z in the QLineEdit and it will undo, but if they try to use the undo menu item, it knows nothing about the recent edits and undoes some earlier application action.I looked at the code for QLineEdit, but it is using internal objects which I could not even find:
void QLineEdit::undo() { Q_D(QLineEdit); d->resetInputMethod(); d->control->undo(); }
Is there a way to tell the QLineEdit to use my undoStack?
@Chris-Bare You could do a check when your undo menu action is triggered to see if the focus is on the QLineEdit, and if so you could then call
yourLineEdit->undo()
directly. This would bypass your stack and act appropriately on the line edit. Same with redo but callredo()
. -
@Chris-Bare You could do a check when your undo menu action is triggered to see if the focus is on the QLineEdit, and if so you could then call
yourLineEdit->undo()
directly. This would bypass your stack and act appropriately on the line edit. Same with redo but callredo()
.@ambershark That would sort-of work, but the undo action would still not show up in the undo list viewer.
-
@ambershark That would sort-of work, but the undo action would still not show up in the undo list viewer.
@Chris-Bare Oh, so you want the whole undo experience managed in your QUndoStack. In that case you are going to have to derive from QLineEdit and implement your undostack there. From what I can tell there is no way to access it inside the base QLineEdit.