Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt QUndoCommand example 1

 

QUndoCommand example 1 is a QUndoCommand example.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppQUndoCommandExample1.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-21T19:59:08
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CppQUndoCommandExample1
TEMPLATE = app


SOURCES += main.cpp\
        qtdialog.cpp \
    qtbuttonincrementcommand.cpp

HEADERS  += qtdialog.h \
    qtbuttonincrementcommand.h

FORMS    += qtdialog.ui

 

 

 

 

 

main.cpp

 

#include <QApplication>
#include "qtdialog.h"

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QtDialog w;
  w.show();
  
  return a.exec();
}

 

 

 

 

 

qtbuttonincrementcommand.h

 

#ifndef QTBUTTONINCREMENTCOMMAND_H
#define QTBUTTONINCREMENTCOMMAND_H

#include <QUndoCommand>

struct QPushButton;

struct QtButtonIncrementCommand : public QUndoCommand
{
  explicit QtButtonIncrementCommand(QPushButton * const button);

  ///Virtual function supplied by QUndoCommand
  void redo();

  ///Virtual function supplied by QUndoCommand
  void undo();

  private:
  QPushButton * const m_button;

};

#endif // QTBUTTONINCREMENTCOMMAND_H

 

 

 

 

 

qtbuttonincrementcommand.cpp

 

#include "qtbuttonincrementcommand.h"

#include <cassert>
#include <QPushButton>

QtButtonIncrementCommand::QtButtonIncrementCommand(
  QPushButton * const button)
  : m_button(button)
{
  assert(m_button);
}

void QtButtonIncrementCommand::redo()
{
  const int current_number = m_button->text().toInt();
  const int new_number = current_number + 1;
  m_button->setText(QString::number(new_number) );
}

void QtButtonIncrementCommand::undo()
{
  const int current_number = m_button->text().toInt();
  const int new_number = current_number - 1;
  m_button->setText(QString::number(new_number) );
}

 

 

 

 

 

qtdialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#include <QDialog>

namespace Ui {
  class QtDialog;
}

struct QUndoStack;

class QtDialog : public QDialog
{
  Q_OBJECT
  
public:
  explicit QtDialog(QWidget *parent = 0);
  ~QtDialog();


  
private slots:
  void keyPressEvent(QKeyEvent *);
  void on_button_clicked();

private:
  Ui::QtDialog *ui;

  QUndoStack * const m_stack;
};

#endif // QTDIALOG_H

 

 

 

 

 

qtdialog.cpp

 

#include "qtdialog.h"

#include <QKeyEvent>
#include <QUndoStack>
#include "qtbuttonincrementcommand.h"

#include "ui_qtdialog.h"

QtDialog::QtDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::QtDialog),
  m_stack(new QUndoStack(this))
{
  ui->setupUi(this);
}

QtDialog::~QtDialog()
{
  delete ui;
}

void QtDialog::keyPressEvent(QKeyEvent * e)
{
  if ( (e->modifiers() & Qt::ControlModifier)
    && !(e->modifiers() & Qt::ShiftModifier)
    && e->key() == Qt::Key_Z)
  {
    m_stack->undo();
  }
  if ( (e->modifiers() & Qt::ControlModifier)
    && (e->modifiers() & Qt::ShiftModifier)
    && e->key() == Qt::Key_Z)
  {
    m_stack->redo();
  }
}

void QtDialog::on_button_clicked()
{
  QtButtonIncrementCommand * const cmd
    = new QtButtonIncrementCommand(ui->button);

  //By pushing the command, redo is called
  m_stack->push(cmd);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml