[<< Contents] [<< Prev] [Next >>]
Not all applications need the ability to allow users to undo & redo commands or actions, but if it is to be implemented it is easier if started early in the application design and implementation cycle. To implement this functionality in our application we will use the Qt Undo Framework.
In this part of the tutorial we will do the preparation work adding an undo stack and undo stack view to our application. This will be done by enhancing our MainWindow class to create the undo stack, add some menu options, and pass a pointer to the undo stack to our Scene class. In the next part of the tutorial we will use this undo stack to hold and implement the undo & redo commands for adding, moving and deleting the radio stations.
Add forward declarations for the classes QUndoStack and QUndoView.
class QUndoStack; class QUndoView; |
After the showMessage declaration add the new slot method showUndoStack. Calling this will open our application undo stack view.
void showUndoStack(); // open up new undo stack window |
After the m_scene variable declaration add the following two new private variables. These will store pointers to our undo stack and undo stack view.
QUndoStack* m_undoStack; // undo stack for undo & redo of commands QUndoView* m_undoView; // undo stack window to view undo & redo commands |
Include the QUndoStack and QUndoView class header files.
#include <QUndoStack> #include <QUndoView> |
In the constructor replace the 6 lines where the menus are added to the menubar with the following. We still add the menus to the menubar but this time we capture the pointers to the edit menu and view menu in local variables. We also create the undo stack, initialise the new private variables, add an "Undo stack" option to the view menu and add undo & redo options to the edit menu.
// add drop down menus menuBar()->addMenu( "&File" ); QMenu* editMenu = menuBar()->addMenu( "&Edit" ); QMenu* viewMenu = menuBar()->addMenu( "&View" ); menuBar()->addMenu( "&Simulate" ); menuBar()->addMenu( "&Help" ); // create undo stack and associated menu actions m_undoStack = new QUndoStack( this ); m_undoView = 0; viewMenu->addAction( "Undo stack", this, SLOT(showUndoStack()) ); QAction* undoAction = m_undoStack->createUndoAction( this ); QAction* redoAction = m_undoStack->createRedoAction( this ); undoAction->setShortcut( QKeySequence::Undo ); redoAction->setShortcut( QKeySequence::Redo ); editMenu->addAction( undoAction ); editMenu->addAction( redoAction ); |
Also in the constructor update the line where the Scene is created to pass a pointer to the undo stack as a parameter.
m_scene = new Scene( m_undoStack ); |
Add the code for the new slot to open the undo stack window. Here the code checks if the view has already been created and if not creates the view and sets the window title. We also set the window attribute WA_QuitOnClose to false so our application does not exit when the undo stack window is closed by the user.
/*********************************** showUndoStack ***********************************/
void MainWindow::showUndoStack()
{
// open up undo stack window
if ( m_undoView == 0 )
{
m_undoView = new QUndoView( m_undoStack );
m_undoView->setWindowTitle( "QSimulate - Undo stack" );
m_undoView->setAttribute( Qt::WA_QuitOnClose, false );
}
m_undoView->show();
}
|
Our Scene class now needs to be updated to accept a pointer to the undo stack as a constructor parameter. Add a forward declaration for the QUndoStack class.
class QUndoStack; |
Update the constructor declaration to have a pointer to a QUndoStack as a parameter.
Scene( QUndoStack* ); // constructor |
Add a private variable which will be used to store the pointer value in this class.
QUndoStack* m_undoStack; // undo stack for undo & redo of commands |
Include the QUndoStack class header file.
#include <QUndoStack> |
Update the constructor to have a pointer to a QUndoStack as a parameter.
Scene::Scene( QUndoStack* undoStack ) : QGraphicsScene() |
In the constructor add the line to store the parameter value into the class private variable.
m_undoStack = undoStack; |
The new code will automatically re-compile when you attempt to run the application. Play with the application and investigate the new menu options that we have added.
[<< Contents] [<< Prev] [Next >>]