From d08325cc3ce8bce2056c80a7598967aa911ecacb Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 9 Mar 2026 12:26:41 +0100 Subject: [PATCH] Model undo stack is accessible in QML. Added Undo & Redo buttons, which are properly en-/disabled and have informative texts. --- ListPage.qml | 49 +++++++++++++++++++++++++++++++++++-------------- main.cpp | 3 +++ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/ListPage.qml b/ListPage.qml index 873c1d3..695a626 100644 --- a/ListPage.qml +++ b/ListPage.qml @@ -5,24 +5,45 @@ import QtQuick.Controls.Material Page { id: page - ListView { - id: listView + ColumnLayout { anchors.fill: parent - focus: true - clip: true - model: mainModel + ListView { + id: listView + Layout.fillWidth: true + Layout.fillHeight: true + focus: true + clip: true - // delegate: ListItemDelegate {} - delegate: ExpandableItemDelegate {} - // delegate: EditableItemDelegate {} - delegateModelAccess: DelegateModel.ReadWrite + model: mainModel - header: bannercomponent - footer: Rectangle { - width: parent.width - height: 30 - gradient: mainGradient + // delegate: ListItemDelegate {} + delegate: ExpandableItemDelegate {} + // delegate: EditableItemDelegate {} + delegateModelAccess: DelegateModel.ReadWrite + + header: bannercomponent + footer: Rectangle { + width: parent.width + height: 30 + gradient: mainGradient + } + } + Button { + Layout.fillWidth: true + text: "Undo: " + appUndoStack.undoText + enabled: appUndoStack.canUndo + onClicked: { + appUndoStack.undo() + } + } + Button { + Layout.fillWidth: true + text: "Redo: " + appUndoStack.redoText + enabled: appUndoStack.canRedo + onClicked: { + appUndoStack.redo() + } } } diff --git a/main.cpp b/main.cpp index 5ed00ff..765e30c 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -18,6 +19,7 @@ int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); std::unique_ptr core = std::make_unique(); std::shared_ptr mainModel = core->getSortFilterModel(); + QUndoStack* undoStack = core->getModelUndoStack(); // qInfo() << "QMLApp Version:" << QMLAPP_VERSION; qInfo() << "core->getString():" << core->toString(); @@ -25,6 +27,7 @@ int main(int argc, char* argv[]) { QQmlApplicationEngine engine; engine.rootContext()->setContextProperty(QStringLiteral("core"), core.get()); engine.rootContext()->setContextProperty(QStringLiteral("mainModel"), mainModel.get()); + engine.rootContext()->setContextProperty(QStringLiteral("appUndoStack"), undoStack); QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app,