Using menusThis example demonstrates simple use of menus (a menu bar and pull-down menus). Qt also supports popup menus, although this example doesn't contain any.
Header file:
/**************************************************************************** ** $Id: qt/menu.h 3.0.6 edited Oct 12 2001 $ ** ** 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 MENU_H #define MENU_H #include <qwidget.h> #include <qmenubar.h> #include <qlabel.h> class MenuExample : public QWidget { Q_OBJECT public: MenuExample( QWidget *parent=0, const char *name=0 ); public slots: void open(); void news(); void save(); void closeDoc(); void undo(); void redo(); void normal(); void bold(); void underline(); void about(); void aboutQt(); void printer(); void file(); void fax(); void printerSetup(); protected: void resizeEvent( QResizeEvent * ); signals: void explain( const QString& ); private: QMenuBar *menu; QLabel *label; bool isBold; bool isUnderline; int boldID, underlineID; }; #endif // MENU_H
Implementation:
/**************************************************************************** ** $Id: qt/menu.cpp 3.0.6 edited Jan 29 2002 $ ** ** 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 "menu.h" #include <qpopupmenu.h> #include <qapplication.h> #include <qmessagebox.h> #include <qpixmap.h> #include <qpainter.h> /* XPM */ static const char * p1_xpm[] = { "16 16 3 1", " c None", ". c #000000000000", "X c #FFFFFFFF0000", " ", " ", " .... ", " .XXXX. ", " .............. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .XXXXXXXXXXXX. ", " .............. ", " "}; /* XPM */ static const char * p2_xpm[] = { "16 16 3 1", " c None", ". c #000000000000", "X c #FFFFFFFFFFFF", " ", " ...... ", " .XXX.X. ", " .XXX.XX. ", " .XXX.XXX. ", " .XXX..... ", " .XXXXXXX. ", " .XXXXXXX. ", " .XXXXXXX. ", " .XXXXXXX. ", " .XXXXXXX. ", " .XXXXXXX. ", " .XXXXXXX. ", " ......... ", " ", " "}; /* XPM */ static const char * p3_xpm[] = { "16 16 3 1", " c None", ". c #000000000000", "X c #FFFFFFFFFFFF", " ", " ", " ......... ", " ........... ", " ........ .. ", " ........... ", " ........... ", " ........... ", " ........... ", " ...XXXXX... ", " ...XXXXX... ", " ...XXXXX... ", " ...XXXXX... ", " ......... ", " ", " "}; /* Auxiliary class to provide fancy menu items with different fonts. Used for the "bold" and "underline" menu items in the options menu. */ class MyMenuItem : public QCustomMenuItem { public: MyMenuItem( const QString& s, const QFont& f ) : string( s ), font( f ){}; ~MyMenuItem(){} void paint( QPainter* p, const QColorGroup& /*cg*/, bool /*act*/, bool /*enabled*/, int x, int y, int w, int h ) { p->setFont ( font ); p->drawText( x, y, w, h, AlignLeft | AlignVCenter | DontClip, string ); } QSize sizeHint() { return QFontMetrics( font ).size( AlignLeft | AlignVCenter | ShowPrefix | DontClip, string ); } private: QString string; QFont font; }; See also Examples. |
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 3.0 | |
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