Set up toolbar with QAction : QAction « Qt « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Qt » QActionScreenshots 
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 mainint argc, char **argv )
{
  QApplication appargc, 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::SdiWindowQWidget *parent : QMainWindowparent )
{
  setAttributeQt::WA_DeleteOnClose );
  setWindowTitletr("%1[*] - %2" ).arg(tr("unnamed")).arg(tr("SDI")) );

  docWidget = new QTextEditthis );
  setCentralWidgetdocWidget );

  connectdocWidget->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)) );
  
  createActions();
  createMenus();
  createToolbars();
  statusBar()->showMessagetr("Done") );
}
  
void SdiWindow::closeEventQCloseEvent *event )
{
  ifisSafeToClose() )
    event->accept();
  else
    event->ignore();
}
  
void SdiWindow::fileNew()
{
  (new SdiWindow())->show();
}

void SdiWindow::helpAbout()
{
  QMessageBox::aboutthis, tr("About SDI"), tr("A single document interface application.") );
}

void SdiWindow::createActions()
{
  newAction = new QActionQIcon(":/images/new.png"), tr("&New")this );
  newAction->setShortcuttr("Ctrl+N") );
  newAction->setStatusTiptr("Create a new document") );
  connectnewAction, SIGNAL(triggered()), this, SLOT(fileNew()) );

  openAction = new QActiontr("&Open")this );
  openAction->setShortcuttr("Ctrl+O") );
  openAction->setStatusTiptr("Open a document") );
  connectopenAction, SIGNAL(triggered()), this, SLOT(fileOpen()) );
 
  saveAction = new QActiontr("&Save")this );
  saveAction->setShortcuttr("Ctrl+S") );
  saveAction->setStatusTiptr("Save the document") );
  connectsaveAction, SIGNAL(triggered()), this, SLOT(fileSave()) );
 
  saveAsAction = new QActiontr("Save &As")this );
  saveAsAction->setStatusTiptr("Save the document as") );
  connectsaveAsAction, SIGNAL(triggered()), this, SLOT(fileSaveAs()) );
 
  closeAction = new QActiontr("&Close")this );
  closeAction->setShortcuttr("Ctrl+W") );
  closeAction->setStatusTiptr("Close this document") );
  connectcloseAction, SIGNAL(triggered()), this, SLOT(close()) );

  exitAction = new QActiontr("E&xit")this );
  exitAction->setShortcuttr("Ctrl+Q") );
  exitAction->setStatusTiptr("Quit the application") );
  connectexitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()) );
  

  cutAction = new QActionQIcon(":/images/cut.png"), tr("Cu&t")this );
  cutAction->setShortcuttr("Ctrl+X") );
  cutAction->setStatusTiptr("Cut") );
  cutAction->setEnabled(false);
  connectdocWidget, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)) );
  connectcutAction, SIGNAL(triggered()), docWidget, SLOT(cut()) );
  
  copyAction = new QActionQIcon(":/images/copy.png"), tr("&Copy")this );
  copyAction->setShortcuttr("Ctrl+C") );
  copyAction->setStatusTiptr("Copy") );
  copyAction->setEnabled(false);
  connectdocWidget, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)) );
  connectcopyAction, SIGNAL(triggered()), docWidget, SLOT(copy()) );


  pasteAction = new QActionQIcon(":/images/paste.png"), tr("&Paste")this );
  pasteAction->setShortcuttr("Ctrl+V") );
  pasteAction->setStatusTiptr("Paste") );
  connectpasteAction, SIGNAL(triggered()), docWidget, SLOT(paste()) );
  

  aboutAction = new QActiontr("&About")this );
  aboutAction->setStatusTiptr("About this application") );
  connectaboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()) );

  aboutQtAction = new QActiontr("About &Qt")this );
  aboutQtAction->setStatusTiptr("About the Qt toolkit") );
  connectaboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()) );
}

void SdiWindow::createMenus()
{
  QMenu *menu;
  
  menu = menuBar()->addMenutr("&File") );
  menu->addActionnewAction );
  menu->addSeparator();
  menu->addActionopenAction );
  menu->addActionsaveAction );
  menu->addActionsaveAsAction );
  menu->addSeparator();
  menu->addActioncloseAction );
  menu->addSeparator();
  menu->addActionexitAction );
  
  menu = menuBar()->addMenutr("&Edit") );
  menu->addActioncutAction );
  menu->addActioncopyAction );
  menu->addActionpasteAction );
  
  menu = menuBar()->addMenutr("&Help") );
  menu->addActionaboutAction );
  menu->addActionaboutQtAction );
}

void SdiWindow::createToolbars()
{
  QToolBar *toolbar;
  
  toolbar = addToolBartr("File") );
  toolbar->addActionnewAction );
  
  toolbar = addToolBartr("Edit") );
  toolbar->addActioncutAction );
  toolbar->addActioncopyAction );
  toolbar->addActionpasteAction );
}



bool SdiWindow::isSafeToClose()
{
  ifisWindowModified() ) 
  {
    switchQMessageBox::warningthis, 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::getOpenFileNamethis );
  iffilename.isEmpty() )
    return;

  ifcurrentFilename.isEmpty() && !docWidget->document()->isModified() )
    loadFilefilename );
  else
  {    
    SdiWindow *window = new SdiWindow();
    window->loadFilefilename );
    window->show();
  }
}

bool SdiWindow::fileSave()
{
  ifcurrentFilename.isEmpty() )
    return fileSaveAs();
  else
    return saveFilecurrentFilename );  
}

bool SdiWindow::fileSaveAs()
{
  QString filename = QFileDialog::getSaveFileNamethis, tr("Save As"), currentFilename );
  iffilename.isEmpty() )
    return false;
  
  return saveFilefilename );
}

bool SdiWindow::saveFileQString filename )
{
  QFile filefilename );
  if!file.openQIODevice::WriteOnly | QIODevice::Text ) )
  {
    QMessageBox::warningthis, tr("SDI"), tr("Failed to save file.") );
    return false;
  }
  
  QTextStream stream&file );
  stream << docWidget->toPlainText();
  
  currentFilename = filename;
  docWidget->document()->setModifiedfalse );
  setWindowTitletr("%1[*] - %2" ).arg(filename).arg(tr("SDI")) );
  
  return true;
}

void SdiWindow::loadFileQString filename )
{
  QFile filefilename );
  if!file.openQIODevice::ReadOnly | QIODevice::Text ) )
  {
    QMessageBox::warningthis, tr("SDI"), tr("Failed to open file.") );
    return;
  }
  
  QTextStream stream&file );
  docWidget->setPlainTextstream.readAll() );

  currentFilename = filename;
  docWidget->document()->setModifiedfalse );  
  setWindowTitletr("%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:
  SdiWindowQWidget *parent = );
  
protected:
  void closeEventQCloseEvent *event );
  
private slots:
  void fileNew();
  void helpAbout();
  
  void fileOpen();
  bool fileSave();
  bool fileSaveAs();

private:
  void createActions();
  void createMenus();
  void createToolbars();

  bool isSafeToClose();

  bool saveFileQString filename );
  void loadFileQString 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

   
    
  
Related examples in the same category
1.Link QAction and button
2.QAction with shortcut
3.Set up Menu bar with QAction
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.