Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Qt QUndoView: Example 1

 

QUndoView: Example 1 is a QUndoView 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: CppQUndoViewExample1.pro

 

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
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) );
  this->setText("Incremented number to " + 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 <cassert>
#include <QKeyEvent>
#include <QUndoStack>
#include <QUndoView>
#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);

  //Add the QUndoView and be done with it
  {
    //Craate the QUndoView
    QUndoView * const view = new QUndoView(this);

    //Let it display the QUndoStack m_stack
    view->setStack(m_stack);

    //Let it have some height
    view->setMinimumHeight(200);

    //Add it to this dialog's layout
    assert(this->layout());
    this->layout()->addWidget(view);
  }
}

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();
    return;
  }
  if ( (e->modifiers() & Qt::ControlModifier)
    && (e->modifiers() & Qt::ShiftModifier)
    && e->key() == Qt::Key_Z)
  {
    m_stack->redo();
    return;
  }
  if ( e->key() == Qt::Key_Escape)
  {
    close();
    return;
  }
}

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