A Complete Application WindowThis example program looks like a complete modern application. It has a menu bar, it has a tool bar, it has a status bar and works like a simple text editor. There is a walkthrough of this example. Header file: /**************************************************************************** ** $Id: //depot/qt/main/examples/application/application.h#5 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #ifndef APPLICATION_H #define APPLICATION_H #include <qmainwindow.h> class QMultiLineEdit; class QToolBar; class QPopupMenu; class ApplicationWindow: public QMainWindow { Q_OBJECT public: ApplicationWindow(); ~ApplicationWindow(); protected: void closeEvent( QCloseEvent* ); private slots: void newDoc(); void load(); void load( const char *fileName ); void save(); void saveAs(); void print(); void about(); void aboutQt(); private: QPrinter *printer; QMultiLineEdit *e; QToolBar *fileTools; QString filename; }; #endif Implementation: /**************************************************************************** ** $Id: //depot/qt/main/examples/application/application.cpp#14 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #include "application.h" #include <qimage.h> #include <qpixmap.h> #include <qtoolbar.h> #include <qtoolbutton.h> #include <qpopupmenu.h> #include <qmenubar.h> #include <qkeycode.h> #include <qmultilineedit.h> #include <qfile.h> #include <qfiledialog.h> #include <qstatusbar.h> #include <qmessagebox.h> #include <qprinter.h> #include <qapplication.h> #include <qaccel.h> #include <qtextstream.h> #include <qpainter.h> #include <qpaintdevicemetrics.h> #include <qwhatsthis.h> #include "filesave.xpm" #include "fileopen.xpm" #include "fileprint.xpm" const char * fileOpenText = "<img source=\"fileopen\"> " "Click this button to open a <em>new file</em>. <br><br>" "You can also select the <b>Open command</b> from the File menu."; const char * fileSaveText = "Click this button to save the file you are " "editing. You will be prompted for a file name.\n\n" "You can also select the Save command from the File menu.\n\n" "Note that implementing this function is left as an exercise for the reader."; const char * filePrintText = "Click this button to print the file you " "are editing.\n\n" "You can also select the Print command from the File menu."; ApplicationWindow::ApplicationWindow() : QMainWindow( 0, "example application main window", WDestructiveClose ) { int id; printer = new QPrinter; QPixmap openIcon, saveIcon, printIcon; fileTools = new QToolBar( this, "file operations" ); fileTools->setLabel( tr( "File Operations" ) ); openIcon = QPixmap( fileopen ); QToolButton * fileOpen = new QToolButton( openIcon, "Open File", QString::null, this, SLOT(load()), fileTools, "open file" ); saveIcon = QPixmap( filesave ); QToolButton * fileSave = new QToolButton( saveIcon, "Save File", QString::null, this, SLOT(save()), fileTools, "save file" ); printIcon = QPixmap( fileprint ); QToolButton * filePrint = new QToolButton( printIcon, "Print File", QString::null, this, SLOT(print()), fileTools, "print file" ); (void)QWhatsThis::whatsThisButton( fileTools ); QWhatsThis::add( fileOpen, fileOpenText ); QMimeSourceFactory::defaultFactory()->setPixmap( "fileopen", openIcon ); QWhatsThis::add( fileSave, fileSaveText ); QWhatsThis::add( filePrint, filePrintText ); QPopupMenu * file = new QPopupMenu( this ); menuBar()->insertItem( "&File", file ); file->insertItem( "&New", this, SLOT(newDoc()), CTRL+Key_N ); id = file->insertItem( openIcon, "&Open", this, SLOT(load()), CTRL+Key_O ); file->setWhatsThis( id, fileOpenText ); id = file->insertItem( saveIcon, "&Save", this, SLOT(save()), CTRL+Key_S ); file->setWhatsThis( id, fileSaveText ); id = file->insertItem( "Save &as...", this, SLOT(saveAs()) ); file->setWhatsThis( id, fileSaveText ); file->insertSeparator(); id = file->insertItem( printIcon, "&Print", this, SLOT(print()), CTRL+Key_P ); file->setWhatsThis( id, filePrintText ); file->insertSeparator(); file->insertItem( "&Close", this, SLOT(close()), CTRL+Key_W ); file->insertItem( "&Quit", qApp, SLOT( closeAllWindows() ), CTRL+Key_Q ); QPopupMenu * help = new QPopupMenu( this ); menuBar()->insertSeparator(); menuBar()->insertItem( "&Help", help ); help->insertItem( "&About", this, SLOT(about()), Key_F1 ); help->insertItem( "About &Qt", this, SLOT(aboutQt()) ); help->insertSeparator(); help->insertItem( "What's &This", this, SLOT(whatsThis()), SHIFT+Key_F1 ); e = new QMultiLineEdit( this, "editor" ); e->setFocus(); setCentralWidget( e ); statusBar()->message( "Ready", 2000 ); resize( 450, 600 ); } ApplicationWindow::~ApplicationWindow() { delete printer; } void Main: /**************************************************************************** ** $Id: //depot/qt/main/examples/application/main.cpp#4 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #include < |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 2.3 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com