博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Qt自带例子中添加HideItem,并实现相应Undo和Redo功能
阅读量:6443 次
发布时间:2019-06-23

本文共 5475 字,大约阅读时间需要 18 分钟。

hot3.png

原工程:tools/undoframework.pro

//==========================mainwindow.h

//add the slot into mainwindow.h

private slots:

//Jingz添加
void hideItem();
//
    void deleteItem();

//add the action into mainwindow.h

private:

    void createActions();
    void createMenus();
    void createUndoView();
//Jingz添加
QAction *hideAction;
//

//==========================mainwindow.cpp

//! [2] initialize the action

void MainWindow::createActions()
{
    deleteAction = new QAction(tr("&Delete Item"), this);
    deleteAction->setShortcut(tr("Del"));
    connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteItem()));
//! [2] //! [3] -------------------------------------------------------------------------- Jingz 
hideAction = new QAction(tr("&Hide Item"),this);
hideAction->setShortcut(tr("Ctrl+H"));
connect(hideAction,SIGNAL(triggered()),this,SLOT(hideItem()));
//! [3] //! [4]

.............

undoAction = undoStack->createUndoAction(this, tr("&Undo"));

    undoAction->setShortcuts(QKeySequence::Undo);
    redoAction = undoStack->createRedoAction(this, tr("&Redo"));
    redoAction->setShortcuts(QKeySequence::Redo);
...............

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);
editMenu->addAction(hideAction);//---------------------------------Jingz add the action into menu

..............................

//! [16] ================Jingz realize hidItem 

void MainWindow::hideItem()
{
if (diagramScene->selectedItems().isEmpty())
return;
QUndoCommand *hideCommand = new HideCommand(diagramScene); 

//operation  to change the item status and keep previous diagramScene in the undoStack 

undoStack->push(hideCommand);//把压制的代码解决就行啦。
}
//! [16]

//! [3]   ================================================commands.h    Jingz

class HideCommand : public QUndoCommand
{
public:
HideCommand(QGraphicsScene *graphicsScene, QUndoCommand *parent = 0);
~HideCommand();
void undo();
void redo();
private:
DiagramItem *myDiagramItem;
QGraphicsScene *myGraphicsScene;
//QList <int> hideGraphics;
//int undoIndex;
};
//! [3]

//! [10]=================================================commands.cpp Jingz

HideCommand::HideCommand(QGraphicsScene *scene,QUndoCommand *parent)
: QUndoCommand(parent)
{
myGraphicsScene = scene;
QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
myGraphicsScene->selectedItems().first()->setVisible(false);
list.first()->setSelected(false);
myDiagramItem = static_cast<DiagramItem *>(list.first());
//QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
//list.first()->setSelected(false);
//myDiagramItem = static_cast<DiagramItem *>(list.first());
//setText(QObject::tr("Delete %1")
// .arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
//if (hideGraphics.count()>undoIndex)//当出现undo后做新修改,覆盖后面的redo记录
//{
// hideGraphics.pop_back();
//}
//for (int i = 0;i < myGraphicsScene->items().count();i++ )
//{
// myDiagramItem = static_cast<DiagramItem *>(myGraphicsScene->items().value(i));
// if (!myGraphicsScene->items().value(i)->isVisible())
// {
// myGraphicsScene->items().value(i)->show();
// setText(QObject::tr("Show %1").arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
// }
//
//}
// QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
/*for (int i = 0;i < myGraphicsScene->selectedItems().count();i++)
{
myGraphicsScene->selectedItems().value(i)->hide();
myDiagramItem = static_cast<DiagramItem *>(myGraphicsScene->selectedItems().value(i));
myGraphicsScene->selectedItems().value(i)->setSelected(false);
hideGraphics.append(i);
undoIndex = hideGraphics.count();
setText(QObject::tr("Hide %1").arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
}*/
// myGraphicsScene->update();
//myGraphicsScene->selectedItems().first()->setSelected(false);
setText(QObject::tr("Hide %1").arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
}
//! [10]
HideCommand::~HideCommand()
{
/*if (!myDiagramItem->scene())
delete myDiagramItem;*/
}
//! [11]
void HideCommand::undo()
{
/*int i = hideGraphics.at(--undoIndex);*///撤销本次操作
//if (myGraphicsScene->items().at(i)->isVisible())
//{
// myGraphicsScene->items().at(i)->hide();
//}
//else
//{
// myGraphicsScene->items().at(i)->show();
//}//
//i = hideGraphics.at(--undoIndex);//恢复现场
//if (myGraphicsScene->items().at(i)->isVisible())
//{
// myGraphicsScene->items().at(i)->hide();
//}
//else
//{
// myGraphicsScene->items().at(i)->show();
//}
// myGraphicsScene->items().at(i)->setVisible(false);
myDiagramItem->setVisible(true);
myGraphicsScene->update();
}
//! [11]
//! [12]
void HideCommand::redo()
{
/*int i = hideGraphics.at(++undoIndex);*/
/*if (myGraphicsScene->items().at(i)->isVisible())
{
myGraphicsScene->items().at(i)->hide();
}
else
{
myGraphicsScene->items().at(i)->show();
}
i = hideGraphics.at(++undoIndex);
if (myGraphicsScene->items().at(i)->isVisible())
{
myGraphicsScene->items().at(i)->hide();
}
else
{
myGraphicsScene->items().at(i)->show();
}*/
/*myGraphicsScene->items().at(i)->setVisible(true);*/
myDiagramItem->setVisible(false);
myGraphicsScene->update();
}
//! [12]
QString createCommandString(DiagramItem *item, const QPointF &pos)
{
    return QObject::tr("%1 at (%2, %3)")
        .arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle")
        .arg(pos.x()).arg(pos.y());
}

转载于:https://my.oschina.net/jingzhe/blog/157668

你可能感兴趣的文章
编写自动化测试用例的原则
查看>>
poj2955(区间dp)
查看>>
突然多了个机会…纠结了一个星期。
查看>>
SAP QUERY
查看>>
MIGO收货 BAPI :BAPI_GOODSMVT_CREATE BADI增强
查看>>
【windows中常用的服务概览和总结】
查看>>
3.插入排序--直接插入排序
查看>>
UVA1584 UVALive3225 Circular Sequence
查看>>
StringBuffer
查看>>
Per-Pixel Lighting
查看>>
外部中断
查看>>
html的dtd声明
查看>>
ivy resolve标签
查看>>
.NET Web后台动态加载Css、JS 文件,换肤方案
查看>>
使用开源软件vlc media player 录制桌面视频
查看>>
web前端(2)—— 前端技术介绍
查看>>
ife2018 零基础学院 day 3
查看>>
Kali Linux信息收集之nbtscan-unixwiz
查看>>
hdu 5476 (计算几何)
查看>>
51 nod 1610 路径计数(Moblus+dp)
查看>>