Walkthrough: A Simple Application with Actions
While reading through the implementation of the ApplicationWindow constructor you have maybe asked yourself: "The fileOpen tool-button in the toolbar does exactly the same thing as the File->Open menu-entry. Their "What's this?" help is the same, the icons common, the same slot is connected to both them ... Shouldn't it be possible to save some code and don't invent the wheel twice?" Indeed, it is. In modern GUI-application programming you will use so called actions to do this. An action collects all the common items (icon, tooltip, menu-entry text, shortcuts, "What's this?" help-text and what to do -- the actual action) together. Whenever this action is required (in the toolbar, as a menu-entry) all the programmer has to do is to insert the action in the respective toolbar or menu. Its appearance (as a tool-button or a menu-entry) is something, the programmer does not has to worry about -- it's obvious from the context. With the QAction class, Qt provides you with everything you need to use this striking concept. So let's write an ApplicationWindow constructor that makes use of actions.
The ApplicationWindow constructor with Actions
Nothing new so far. But with the next lines...
... the difference becomes obvious. Here we define the actions our application
is supposed to undertake: it should create a new editor-instance (fileNewAction),
open a file, save a file, save it under a different name, print the
content of the editor, close an editor window and quit the entire application.
The first one has the name new and can be reached via the accelerator Ctrl+N.
When used as a menu-entry it will provide the entry New and can be reached
via the accelerator Alt-N (&N). As we won't set a special tooltip-text, the
text New with the accelerator Ctrl+N in brackets will show up when
a user holds the mouse over a tool-button and does nothing.
When the action becomes activated (the user chooses the respective
menu-entry or clicks an appropriate tool-button), it connects to the
newDoc() slot.
The same way we create an Open File action and connect its activated() signal
to the choose() slot.
There is however a novelty: the fileOpenAction (unlike fileNewAction)
is assigned a pixmap (the one included with the fileopen.xpm file).
For the fileOpenAction we want to provide "What's This?" help and therefore
define an appropriate rich-text.
As fileOpenText makes use of a pixmap, we have to inform the rich-text
engine that it should provide the pixmap defined for fileOpenAction
whenever a rich-text
asks for an image-source named fileopen.
The slightly complex procedure to gain the pixmap from the action is
due to the fact that a QAction is not simply assigned a pixmap but an
entire iconset. A QIconSet provides up to six pixmaps suited for
different sizes (large, small) and modes (active, disabled etc.).
As we initially fed fileOpenAction with just one pixmap its iconset
will be calculated from it automatically.
For simplicity reasons we want the icon in the "What's this?" text to be the same we
used in the fileOpenAction constructor. This is done by using
QIconSet::pixmap() upon fileOpenAction's iconSet().
Finally we assign "What's this?" help to the fileOpenAction.
The same way we create a Save File action with a pixmap, "What's this?" help
and the more common items like menu-entry text and accelerator. Note
that we don't have to bother with the rich-text engine because the
pixmap is not used in fileSaveText. When activated
the fileSaveAction will call the
save() slot.
For the Save File As action we reuse fileSaveText but do without a
pixmap. On activation, this action calls the
saveAs() slot.
The Print File action -- with an activated() signal connected to
print() -- looks very much
like fileSaveText.
For the last two actions, fileCloseAction and fileQuitAction, we do it the
easy way: no "What's this?", no pixmaps. Thus we have defined all the
actions we need.
The only thing left is to use them as menu- and toolbar-entries.
First we create a toolbar in this window
and define a caption for it.
As actions that weren't assigned a pixmap are quite useless in a toolbar
we'll restrict ourselves to three tool-buttons for opening, saving
and printing files.
The first tool-button is easily installed: All we have to do
is to add the fileOpenAction to the fileTools toolbar.
The same easy procedure applies to fileSaveAction and filePrintAction.
To provide the user with a means to toggle his or her mouse in "What's this?" mode,
we need a fourth icon in the toolbar: the (predefined) "What's this?" button.
Next we install the newly created file popup-menu in the menu bar.
After we're done with this, we populate the menu ...
... with some menu-entries derived from actions, ...
... a separator ...
... and more actions and separators.
The rest of the constructor ...
... is exactly the same as in the
tool-button and menu-entry version.
See also Step-by-step Examples.
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Quarterly au hasardRequête de données génériques avec QtXmlPatterns et XQueryQt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
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