/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include "mainwindow.h"
#include "diagramscene.h"
#include "diagramitem.h"
#include "movecommand.h"
//! [0]
MainWindow::MainWindow()
{
undoStack = new QUndoStack();
createActions();
createMenus();
createUndoView();
diagramScene = new DiagramScene();
QBrush pixmapBrush(QPixmap(":/images/cross.png").scaled(30, 30));
diagramScene->setBackgroundBrush(pixmapBrush);
diagramScene->setSceneRect(QRect(0, 0, 500, 500));
connect(diagramScene, SIGNAL(itemMoved(DiagramItem*,QPointF)),
this, SLOT(itemMoved(DiagramItem*,QPointF)));
setWindowTitle("Undo Framework");
QGraphicsView *view = new QGraphicsView(diagramScene);
setCentralWidget(view);
resize(700, 500);
}
//! [0]
//! [1]
void MainWindow::createUndoView()
{
undoView = new QUndoView(undoStack);
undoView->setWindowTitle(tr("Command List"));
undoView->show();
undoView->setAttribute(Qt::WA_QuitOnClose, false);
}
//! [1]
//! [2]
void MainWindow::createActions()
{
deleteAction = new QAction(tr("&Delete Item"), this);
deleteAction->setShortcut(tr("Del"));
connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
//! [2] //! [3]
//! [3] //! [4]
addBoxAction = new QAction(tr("Add &Box"), this);
//! [4]
addBoxAction->setShortcut(tr("Ctrl+O"));
connect(addBoxAction, SIGNAL(triggered()), this, SLOT(addBox()));
addTriangleAction = new QAction(tr("Add &Triangle"), this);
addTriangleAction->setShortcut(tr("Ctrl+T"));
connect(addTriangleAction, SIGNAL(triggered()), this, SLOT(addTriangle()));
//! [5]
undoAction = undoStack->createUndoAction(this, tr("&Undo"));
undoAction->setShortcuts(QKeySequence::Undo);
redoAction = undoStack->createRedoAction(this, tr("&Redo"));
redoAction->setShortcuts(QKeySequence::Redo);
//! [5]
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
aboutAction = new QAction(tr("&About"), this);
QList<QKeySequence> aboutShortcuts;
aboutShortcuts << tr("Ctrl+A") << tr("Ctrl+B");
aboutAction->setShortcuts(aboutShortcuts);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
}
//! [6]
void MainWindow::createMenus()
{
//! [6]
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(exitAction);
//! [7]
editMenu = menuBar()->addMenu(tr("&Edit"));
editMenu->addAction(undoAction);
editMenu->addAction(redoAction);
editMenu->addSeparator();
editMenu->addAction(deleteAction);
connect(editMenu, SIGNAL(aboutToShow()),
this, SLOT(itemMenuAboutToShow()));
connect(editMenu, SIGNAL(aboutToHide()),
this, SLOT(itemMenuAboutToHide()));
//! [7]
itemMenu = menuBar()->addMenu(tr("&Item"));
itemMenu->addAction(addBoxAction);
itemMenu->addAction(addTriangleAction);
helpMenu = menuBar()->addMenu(tr("&About"));
helpMenu->addAction(aboutAction);
//! [8]
}
//! [8]
//! [9]
void MainWindow::itemMoved(DiagramItem *movedItem,
const QPointF &oldPosition)
{
undoStack->push(new MoveCommand(movedItem, oldPosition));
}
//! [9]
//! [10]
void MainWindow::deleteItem()
{
if (diagramScene->selectedItems().isEmpty())
return;
QUndoCommand *deleteCommand = new DeleteCommand(diagramScene);
undoStack->push(deleteCommand);
}
//! [10]
//! [11]
void MainWindow::itemMenuAboutToHide()
{
deleteAction->setEnabled(true);
}
//! [11]
//! [12]
void MainWindow::itemMenuAboutToShow()
{
deleteAction->setEnabled(!diagramScene->selectedItems().isEmpty());
}
//! [12]
//! [13]
void MainWindow::addBox()
{
QUndoCommand *addCommand = new AddCommand(DiagramItem::Box, diagramScene);
undoStack->push(addCommand);
}
//! [13]
//! [14]
void MainWindow::addTriangle()
{
QUndoCommand *addCommand = new AddCommand(DiagramItem::Triangle,
diagramScene);
undoStack->push(addCommand);
}
//! [14]
//! [15]
void MainWindow::about()
{
QMessageBox::about(this, tr("About Undo"),
tr("The <b>Undo</b> example demonstrates how to "
"use Qt's undo framework."));
}
//! [15]
没有合适的资源?快使用搜索试试~ 我知道了~
Qt撤销恢复功能经典例子(QUndoStack、QCommands、QUndoGroup)
共23个文件
cpp:10个
h:6个
qrc:1个
1星 需积分: 50 97 下载量 89 浏览量
2019-07-30
13:51:31
上传
评论 2
收藏 22KB RAR 举报
温馨提示
Qt撤销恢复功能经典例子(QUndoStack、QCommands、QUndoGroup)。 该例子典型的介绍了Qt中如何使用并添加撤销恢复功能。(Vs+Qt)
资源推荐
资源详情
资源评论
收起资源包目录
Qt撤销恢复.rar (23个子文件)
UndoTest
UndoTest.sln 890B
UndoTest
MainWindow.h 3KB
MoveCommand.h 3KB
UndoTest.vcproj.vspscc 753B
DiagramItem.h 3KB
DiagramScene.cpp 3KB
DiagramItem.cpp 3KB
Resources
undotest.qrc 68B
undotest.h 296B
GeneratedFiles
Release
ui_undotest.h 2KB
Debug
moc_DiagramScene.cpp 3KB
moc_MainWindow.cpp 3KB
moc_undotest.cpp 2KB
qrc_undotest.cpp 719B
undotest.cpp 170B
UndoTest.vcproj.PC2011102520QZL.Administrator.user 1KB
MoveCommand.cpp 5KB
undotest.ui 795B
DiagramScene.h 3KB
MainWindow.cpp 6KB
main.cpp 252B
UndoTest.vcproj 15KB
UndoTest.suo 17KB
共 23 条
- 1
资源评论
- 繁星蓝雨2020-05-25就是官方demo。。。。
- candy_HC2022-12-28一垃圾玩意儿,我*** #毫无价值
信必诺
- 粉丝: 2w+
- 资源: 150
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功