C++/Qt/QAction

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Link QAction and button

  
Foundations of Qt Development\Chapter04\sdi\main.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QApplication>
#include "sdiwindow.h"
int main( int argc, char **argv )
{
  QApplication app( argc, argv );
  
  (new SdiWindow)->show();
  
  return app.exec();
}

Foundations of Qt Development\Chapter04\sdi\sdiwindow.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QApplication>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QCloseEvent>
#include <QMessageBox>
#include <QTextEdit>
#include "sdiwindow.h"
SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent )
{
  setAttribute( Qt::WA_DeleteOnClose );
  setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) );
  docWidget = new QTextEdit( this );
  setCentralWidget( docWidget );
  connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) );
  
  createActions();
  createMenus();
  createToolbars();
  statusBar()->showMessage( tr("Done") );
}
  
void SdiWindow::closeEvent( QCloseEvent *event )
{
  if( isSafeToClose() )
    event->accept();
  else
    event->ignore();
}
bool SdiWindow::isSafeToClose()
{
  if( isWindowModified() ) 
  {
    switch( QMessageBox::warning( this, tr("SDI"), 
      tr("The document has unsaved changes.\n"
         "Do you want to save it before it is closed?"), 
         QMessageBox::Discard | QMessageBox::Cancel ) )
    {
    case QMessageBox::Cancel:
      return false;
    default:
      return true;
    }
  }
  return true;
}
  
void SdiWindow::fileNew()
{
  (new SdiWindow())->show();
}
void SdiWindow::helpAbout()
{
  QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") );
}
void SdiWindow::createActions()
{
  newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this );
  newAction->setShortcut( tr("Ctrl+N") );
  newAction->setStatusTip( tr("Create a new document") );
  connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) );
  closeAction = new QAction( tr("&Close"), this );
  closeAction->setShortcut( tr("Ctrl+W") );
  closeAction->setStatusTip( tr("Close this document") );
  connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) );
  exitAction = new QAction( tr("E&xit"), this );
  exitAction->setShortcut( tr("Ctrl+Q") );
  exitAction->setStatusTip( tr("Quit the application") );
  connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) );
  
  cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this );
  cutAction->setShortcut( tr("Ctrl+X") );
  cutAction->setStatusTip( tr("Cut") );
  cutAction->setEnabled(false);
  connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) );
  connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) );
  
  copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this );
  copyAction->setShortcut( tr("Ctrl+C") );
  copyAction->setStatusTip( tr("Copy") );
  copyAction->setEnabled(false);
  connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) );
  connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) );
  pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this );
  pasteAction->setShortcut( tr("Ctrl+V") );
  pasteAction->setStatusTip( tr("Paste") );
  connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) );
  
  aboutAction = new QAction( tr("&About"), this );
  aboutAction->setStatusTip( tr("About this application") );
  connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) );
  aboutQtAction = new QAction( tr("About &Qt"), this );
  aboutQtAction->setStatusTip( tr("About the Qt toolkit") );
  connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) );
}
void SdiWindow::createMenus()
{
  QMenu *menu;
  
  menu = menuBar()->addMenu( tr("&File") );
  menu->addAction( newAction );
  menu->addAction( closeAction );
  menu->addSeparator();
  menu->addAction( exitAction );
  
  menu = menuBar()->addMenu( tr("&Edit") );
  menu->addAction( cutAction );
  menu->addAction( copyAction );
  menu->addAction( pasteAction );
  
  menu = menuBar()->addMenu( tr("&Help") );
  menu->addAction( aboutAction );
  menu->addAction( aboutQtAction );
}
void SdiWindow::createToolbars()
{
  QToolBar *toolbar;
  
  toolbar = addToolBar( tr("File") );
  toolbar->addAction( newAction );
  
  toolbar = addToolBar( tr("Edit") );
  toolbar->addAction( cutAction );
  toolbar->addAction( copyAction );
  toolbar->addAction( pasteAction );
}


Foundations of Qt Development\Chapter04\sdi\sdiwindow.h
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#ifndef SDIWINDOW_H
#define SDIWINDOW_H
#include <QMainWindow>
class QAction;
class QTextEdit;
class SdiWindow : public QMainWindow
{
  Q_OBJECT
  
public:
  SdiWindow( QWidget *parent = 0 );
  
protected:
  void closeEvent( QCloseEvent *event );
  
private slots:
  void fileNew();
  void helpAbout();
private:
  void createActions();
  void createMenus();
  void createToolbars();
  bool isSafeToClose();
  QTextEdit *docWidget;
  
  QAction *newAction;
  QAction *closeAction;
  QAction *exitAction;
  
  QAction *cutAction;
  QAction *copyAction;
  QAction *pasteAction;
  
  QAction *aboutAction;
  QAction *aboutQtAction;
};
  
#endif // SDIWINDOW_H


QAction with shortcut

  
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
class MainWindow : public QMainWindow
{
public:
    MainWindow();
    QAction *newAct;
};
MainWindow()
{
    newAct = new QAction(tr("&New"), this);
    newAct->setShortcut(tr("Ctrl+N"));
    newAct->setStatusTip(tr("Create a new file"));
    newAct->setWhatsThis(tr("Click this option to create a new file."));
}
int main()
{
    return 0;
}


Set up Menu bar with QAction

  
Foundations of Qt Development\Chapter08\readwriteapplication\main.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QApplication>
#include "sdiwindow.h"
int main( int argc, char **argv )
{
  QApplication app( argc, argv );
  
  (new SdiWindow)->show();
  QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
  
  return app.exec();
}

Foundations of Qt Development\Chapter08\readwriteapplication\sdiwindow.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QApplication>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QCloseEvent>
#include <QMessageBox>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include "sdiwindow.h"
SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent )
{
  setAttribute( Qt::WA_DeleteOnClose );
  setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) );
  docWidget = new QTextEdit( this );
  setCentralWidget( docWidget );
  connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) );
  
  createActions();
  createMenus();
  createToolbars();
  statusBar()->showMessage( tr("Done") );
}
  
void SdiWindow::closeEvent( QCloseEvent *event )
{
  if( isSafeToClose() )
    event->accept();
  else
    event->ignore();
}
  
void SdiWindow::fileNew()
{
  (new SdiWindow())->show();
}
void SdiWindow::helpAbout()
{
  QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") );
}
void SdiWindow::createActions()
{
  newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this );
  newAction->setShortcut( tr("Ctrl+N") );
  newAction->setStatusTip( tr("Create a new document") );
  connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) );
  openAction = new QAction( tr("&Open"), this );
  openAction->setShortcut( tr("Ctrl+O") );
  openAction->setStatusTip( tr("Open a document") );
  connect( openAction, SIGNAL(triggered()), this, SLOT(fileOpen()) );
 
  saveAction = new QAction( tr("&Save"), this );
  saveAction->setShortcut( tr("Ctrl+S") );
  saveAction->setStatusTip( tr("Save the document") );
  connect( saveAction, SIGNAL(triggered()), this, SLOT(fileSave()) );
 
  saveAsAction = new QAction( tr("Save &As"), this );
  saveAsAction->setStatusTip( tr("Save the document as") );
  connect( saveAsAction, SIGNAL(triggered()), this, SLOT(fileSaveAs()) );
 
  closeAction = new QAction( tr("&Close"), this );
  closeAction->setShortcut( tr("Ctrl+W") );
  closeAction->setStatusTip( tr("Close this document") );
  connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) );
  exitAction = new QAction( tr("E&xit"), this );
  exitAction->setShortcut( tr("Ctrl+Q") );
  exitAction->setStatusTip( tr("Quit the application") );
  connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) );
  
  cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this );
  cutAction->setShortcut( tr("Ctrl+X") );
  cutAction->setStatusTip( tr("Cut") );
  cutAction->setEnabled(false);
  connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) );
  connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) );
  
  copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this );
  copyAction->setShortcut( tr("Ctrl+C") );
  copyAction->setStatusTip( tr("Copy") );
  copyAction->setEnabled(false);
  connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) );
  connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) );

  pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this );
  pasteAction->setShortcut( tr("Ctrl+V") );
  pasteAction->setStatusTip( tr("Paste") );
  connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) );
  
  aboutAction = new QAction( tr("&About"), this );
  aboutAction->setStatusTip( tr("About this application") );
  connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) );
  aboutQtAction = new QAction( tr("About &Qt"), this );
  aboutQtAction->setStatusTip( tr("About the Qt toolkit") );
  connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) );
}
void SdiWindow::createMenus()
{
  QMenu *menu;
  
  menu = menuBar()->addMenu( tr("&File") );
  menu->addAction( newAction );
  menu->addSeparator();
  menu->addAction( openAction );
  menu->addAction( saveAction );
  menu->addAction( saveAsAction );
  menu->addSeparator();
  menu->addAction( closeAction );
  menu->addSeparator();
  menu->addAction( exitAction );
  
  menu = menuBar()->addMenu( tr("&Edit") );
  menu->addAction( cutAction );
  menu->addAction( copyAction );
  menu->addAction( pasteAction );
  
  menu = menuBar()->addMenu( tr("&Help") );
  menu->addAction( aboutAction );
  menu->addAction( aboutQtAction );
}
void SdiWindow::createToolbars()
{
  QToolBar *toolbar;
  
  toolbar = addToolBar( tr("File") );
  toolbar->addAction( newAction );
  
  toolbar = addToolBar( tr("Edit") );
  toolbar->addAction( cutAction );
  toolbar->addAction( copyAction );
  toolbar->addAction( pasteAction );
}

bool SdiWindow::isSafeToClose()
{
  if( isWindowModified() ) 
  {
    switch( QMessageBox::warning( this, tr("SDI"), 
      tr("The document has unsaved changes.\n"
         "Do you want to save it before it is closed?"), 
          QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ) )
    {
    case QMessageBox::Cancel:
      return false;
    case QMessageBox::Save:
      return fileSave();
    default:
      return true;
    }
  }
  return true;
}
void SdiWindow::fileOpen()
{
  QString filename = QFileDialog::getOpenFileName( this );
  if( filename.isEmpty() )
    return;
  if( currentFilename.isEmpty() && !docWidget->document()->isModified() )
    loadFile( filename );
  else
  {    
    SdiWindow *window = new SdiWindow();
    window->loadFile( filename );
    window->show();
  }
}
bool SdiWindow::fileSave()
{
  if( currentFilename.isEmpty() )
    return fileSaveAs();
  else
    return saveFile( currentFilename );  
}
bool SdiWindow::fileSaveAs()
{
  QString filename = QFileDialog::getSaveFileName( this, tr("Save As"), currentFilename );
  if( filename.isEmpty() )
    return false;
  
  return saveFile( filename );
}
bool SdiWindow::saveFile( QString filename )
{
  QFile file( filename );
  if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
  {
    QMessageBox::warning( this, tr("SDI"), tr("Failed to save file.") );
    return false;
  }
  
  QTextStream stream( &file );
  stream << docWidget->toPlainText();
  
  currentFilename = filename;
  docWidget->document()->setModified( false );
  setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) );
  
  return true;
}
void SdiWindow::loadFile( QString filename )
{
  QFile file( filename );
  if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
  {
    QMessageBox::warning( this, tr("SDI"), tr("Failed to open file.") );
    return;
  }
  
  QTextStream stream( &file );
  docWidget->setPlainText( stream.readAll() );
  currentFilename = filename;
  docWidget->document()->setModified( false );  
  setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) );
}

Foundations of Qt Development\Chapter08\readwriteapplication\sdiwindow.h
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#ifndef SDIWINDOW_H
#define SDIWINDOW_H
#include <QMainWindow>
class QAction;
class QTextEdit;
class SdiWindow : public QMainWindow
{
  Q_OBJECT
  
public:
  SdiWindow( QWidget *parent = 0 );
  
protected:
  void closeEvent( QCloseEvent *event );
  
private slots:
  void fileNew();
  void helpAbout();
  
  void fileOpen();
  bool fileSave();
  bool fileSaveAs();
private:
  void createActions();
  void createMenus();
  void createToolbars();
  bool isSafeToClose();
  bool saveFile( QString filename );
  void loadFile( QString filename );
  QString currentFilename;
  QTextEdit *docWidget;
  
  QAction *newAction;
  QAction *openAction;
  QAction *saveAction;
  QAction *saveAsAction;
  QAction *closeAction;
  QAction *exitAction;
  
  QAction *cutAction;
  QAction *copyAction;
  QAction *pasteAction;
  
  QAction *aboutAction;
  QAction *aboutQtAction;
};
  
#endif // SDIWINDOW_H


Set up toolbar with QAction

  

Foundations of Qt Development\Chapter08\readwriteapplication\main.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QApplication>
#include "sdiwindow.h"
int main( int argc, char **argv )
{
  QApplication app( argc, argv );
  
  (new SdiWindow)->show();
  QObject::connect( &app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()) );
  
  return app.exec();
}

Foundations of Qt Development\Chapter08\readwriteapplication\sdiwindow.cpp
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <QApplication>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QCloseEvent>
#include <QMessageBox>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include "sdiwindow.h"
SdiWindow::SdiWindow( QWidget *parent ) : QMainWindow( parent )
{
  setAttribute( Qt::WA_DeleteOnClose );
  setWindowTitle( tr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) );
  docWidget = new QTextEdit( this );
  setCentralWidget( docWidget );
  connect( docWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) );
  
  createActions();
  createMenus();
  createToolbars();
  statusBar()->showMessage( tr("Done") );
}
  
void SdiWindow::closeEvent( QCloseEvent *event )
{
  if( isSafeToClose() )
    event->accept();
  else
    event->ignore();
}
  
void SdiWindow::fileNew()
{
  (new SdiWindow())->show();
}
void SdiWindow::helpAbout()
{
  QMessageBox::about( this, tr("About SDI"), tr("A single document interface application.") );
}
void SdiWindow::createActions()
{
  newAction = new QAction( QIcon(":/images/new.png"), tr("&New"), this );
  newAction->setShortcut( tr("Ctrl+N") );
  newAction->setStatusTip( tr("Create a new document") );
  connect( newAction, SIGNAL(triggered()), this, SLOT(fileNew()) );
  openAction = new QAction( tr("&Open"), this );
  openAction->setShortcut( tr("Ctrl+O") );
  openAction->setStatusTip( tr("Open a document") );
  connect( openAction, SIGNAL(triggered()), this, SLOT(fileOpen()) );
 
  saveAction = new QAction( tr("&Save"), this );
  saveAction->setShortcut( tr("Ctrl+S") );
  saveAction->setStatusTip( tr("Save the document") );
  connect( saveAction, SIGNAL(triggered()), this, SLOT(fileSave()) );
 
  saveAsAction = new QAction( tr("Save &As"), this );
  saveAsAction->setStatusTip( tr("Save the document as") );
  connect( saveAsAction, SIGNAL(triggered()), this, SLOT(fileSaveAs()) );
 
  closeAction = new QAction( tr("&Close"), this );
  closeAction->setShortcut( tr("Ctrl+W") );
  closeAction->setStatusTip( tr("Close this document") );
  connect( closeAction, SIGNAL(triggered()), this, SLOT(close()) );
  exitAction = new QAction( tr("E&xit"), this );
  exitAction->setShortcut( tr("Ctrl+Q") );
  exitAction->setStatusTip( tr("Quit the application") );
  connect( exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) );
  
  cutAction = new QAction( QIcon(":/images/cut.png"), tr("Cu&t"), this );
  cutAction->setShortcut( tr("Ctrl+X") );
  cutAction->setStatusTip( tr("Cut") );
  cutAction->setEnabled(false);
  connect( docWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) );
  connect( cutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) );
  
  copyAction = new QAction( QIcon(":/images/copy.png"), tr("&Copy"), this );
  copyAction->setShortcut( tr("Ctrl+C") );
  copyAction->setStatusTip( tr("Copy") );
  copyAction->setEnabled(false);
  connect( docWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) );
  connect( copyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) );

  pasteAction = new QAction( QIcon(":/images/paste.png"), tr("&Paste"), this );
  pasteAction->setShortcut( tr("Ctrl+V") );
  pasteAction->setStatusTip( tr("Paste") );
  connect( pasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) );
  
  aboutAction = new QAction( tr("&About"), this );
  aboutAction->setStatusTip( tr("About this application") );
  connect( aboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) );
  aboutQtAction = new QAction( tr("About &Qt"), this );
  aboutQtAction->setStatusTip( tr("About the Qt toolkit") );
  connect( aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) );
}
void SdiWindow::createMenus()
{
  QMenu *menu;
  
  menu = menuBar()->addMenu( tr("&File") );
  menu->addAction( newAction );
  menu->addSeparator();
  menu->addAction( openAction );
  menu->addAction( saveAction );
  menu->addAction( saveAsAction );
  menu->addSeparator();
  menu->addAction( closeAction );
  menu->addSeparator();
  menu->addAction( exitAction );
  
  menu = menuBar()->addMenu( tr("&Edit") );
  menu->addAction( cutAction );
  menu->addAction( copyAction );
  menu->addAction( pasteAction );
  
  menu = menuBar()->addMenu( tr("&Help") );
  menu->addAction( aboutAction );
  menu->addAction( aboutQtAction );
}
void SdiWindow::createToolbars()
{
  QToolBar *toolbar;
  
  toolbar = addToolBar( tr("File") );
  toolbar->addAction( newAction );
  
  toolbar = addToolBar( tr("Edit") );
  toolbar->addAction( cutAction );
  toolbar->addAction( copyAction );
  toolbar->addAction( pasteAction );
}

bool SdiWindow::isSafeToClose()
{
  if( isWindowModified() ) 
  {
    switch( QMessageBox::warning( this, tr("SDI"), 
      tr("The document has unsaved changes.\n"
         "Do you want to save it before it is closed?"), 
          QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel ) )
    {
    case QMessageBox::Cancel:
      return false;
    case QMessageBox::Save:
      return fileSave();
    default:
      return true;
    }
  }
  return true;
}
void SdiWindow::fileOpen()
{
  QString filename = QFileDialog::getOpenFileName( this );
  if( filename.isEmpty() )
    return;
  if( currentFilename.isEmpty() && !docWidget->document()->isModified() )
    loadFile( filename );
  else
  {    
    SdiWindow *window = new SdiWindow();
    window->loadFile( filename );
    window->show();
  }
}
bool SdiWindow::fileSave()
{
  if( currentFilename.isEmpty() )
    return fileSaveAs();
  else
    return saveFile( currentFilename );  
}
bool SdiWindow::fileSaveAs()
{
  QString filename = QFileDialog::getSaveFileName( this, tr("Save As"), currentFilename );
  if( filename.isEmpty() )
    return false;
  
  return saveFile( filename );
}
bool SdiWindow::saveFile( QString filename )
{
  QFile file( filename );
  if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
  {
    QMessageBox::warning( this, tr("SDI"), tr("Failed to save file.") );
    return false;
  }
  
  QTextStream stream( &file );
  stream << docWidget->toPlainText();
  
  currentFilename = filename;
  docWidget->document()->setModified( false );
  setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) );
  
  return true;
}
void SdiWindow::loadFile( QString filename )
{
  QFile file( filename );
  if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
  {
    QMessageBox::warning( this, tr("SDI"), tr("Failed to open file.") );
    return;
  }
  
  QTextStream stream( &file );
  docWidget->setPlainText( stream.readAll() );
  currentFilename = filename;
  docWidget->document()->setModified( false );  
  setWindowTitle( tr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) );
}

Foundations of Qt Development\Chapter08\readwriteapplication\sdiwindow.h
/*
 * Copyright (c) 2006-2007, Johan Thelin
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,  
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *     * Neither the name of APress nor the names of its contributors 
 *       may be used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#ifndef SDIWINDOW_H
#define SDIWINDOW_H
#include <QMainWindow>
class QAction;
class QTextEdit;
class SdiWindow : public QMainWindow
{
  Q_OBJECT
  
public:
  SdiWindow( QWidget *parent = 0 );
  
protected:
  void closeEvent( QCloseEvent *event );
  
private slots:
  void fileNew();
  void helpAbout();
  
  void fileOpen();
  bool fileSave();
  bool fileSaveAs();
private:
  void createActions();
  void createMenus();
  void createToolbars();
  bool isSafeToClose();
  bool saveFile( QString filename );
  void loadFile( QString filename );
  QString currentFilename;
  QTextEdit *docWidget;
  
  QAction *newAction;
  QAction *openAction;
  QAction *saveAction;
  QAction *saveAsAction;
  QAction *closeAction;
  QAction *exitAction;
  
  QAction *cutAction;
  QAction *copyAction;
  QAction *pasteAction;
  
  QAction *aboutAction;
  QAction *aboutQtAction;
};
  
#endif // SDIWINDOW_H