gpt4 book ai didi

c++ - 在 Qt 中撤消重做

转载 作者:行者123 更新时间:2023-11-28 06:46:33 25 4
gpt4 key购买 nike

我在我的 Qt 应用程序中添加了一个图形 View ,其中可以添加不同的项目,如线条、椭圆 如何在图形 View 中应用撤消重做操作。以下是添加行等项目的代码。主窗口.cpp

connect(ui->lineButton, SIGNAL(clicked()), this, SLOT(drawLine()));

行.cpp

void line::mousePressEvent(QGraphicsSceneMouseEvent* e){
if(e->button()==Qt::LeftButton) {
if(mFirstClick){
x1 = e->pos().x();
y1 = e->pos().y();
mFirstClick = false;
mSecondClick = true;
}

else if(!mFirstClick && mSecondClick){
x2 = e->pos().x();
y2 = e->pos().y();
mPaintFlag = true;
mSecondClick = false;
update();
}
}
QGraphicsItem::mousePressEvent(e);
update();
}

void line:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
QRectF rect = boundingRect();
if(mPaintFlag){
QPen paintpen(Qt::red);
paintpen.setWidth(4);

QPen linepen(Qt::black);
linepen.setWidth(1);

QPoint p1;
p1.setX(x1);
p1.setY(y1);

painter->setPen(paintpen);
painter->drawPoint(p1);

QPoint p2;
p2.setX(x2);
p2.setY(y2);

painter->setPen(paintpen);
painter->drawPoint(p2);

painter->setPen(linepen);
painter->drawLine(p1, p2);
}
}

void line::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
if (e->modifiers() & Qt::ShiftModifier) {
stuff << e->pos();
update();
return;
}
QGraphicsItem::mouseMoveEvent(e);
}

void line::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
QGraphicsItem::mouseReleaseEvent(e);
update();
}

画线槽

 void MainWindow::drawLine(){
ui->graphicsView->setScene(scene);
line *item = new line;
scene->addItem(item);
qDebug() << "Line Created";

}

编辑:

void MainWindow::on_actionUndo_triggered()
{
undoView = new QUndoView(undoStack);
ui->actionUndo= undoStack->createUndoAction(this, tr("&Undo"));
ui->actionUndo->setShortcuts(QKeySequence::Undo);
}

最佳答案

您可以继续在您的应用程序中实现命令模式。这是在 The Gang of Four Design Patterns: Elements of Reusable Object-Oriented Software 一书中找到的模式。我认为可以在这里找到比我想到的模式更好的解释:Command Pattern .

虽然本文的重点是游戏,但您不妨将其应用到您的代码中。祝你好运!

关于c++ - 在 Qt 中撤消重做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24894690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com