HelpsystemThis example demonstrates the different Qt classes that can be used to provide context sensitive help in an application. It uses QToolTip and QWhatsThis to provide both static and dynamic balloon help for the widgets in the application, and QToolTipGroup to display extended information for each tooltip in the statusbar. QAssistantClient is used to display help pages using Qt Assistant. The application has a user interface based on a QMainWindow with a menubar, statusbar and a toolbar, and uses a QTable as the central widget. class HeaderToolTip : public QToolTip { public: HeaderToolTip( QHeader *header, QToolTipGroup *group = 0 ); protected: void maybeTip ( const QPoint &p ); }; Two QToolTip subclasses implement dynamic tooltips for QHeader and QTable by reimplementing maybeTip(). The constructors differ from the QToolTip constructor in having a QHeader and a QTable respectively as the first parameter for the constructor instead of a QWidget. This is because we want to ensure that only headers and tables can be passed as arguments. A QToolTipGroup can be provided as the second argument to show tooltips in, for example a statusbar.
class TableToolTip : public QToolTip { public: TableToolTip( QTable* table, QToolTipGroup *group = 0 ); protected: void maybeTip( const QPoint &p ); private: QTable *table; }; The TableToolTip class keeps a reference to the QTable as a member for easier access of the QTable object later on.
HeaderToolTip::HeaderToolTip( QHeader *header, QToolTipGroup *group ) : QToolTip( header, group ) { } The HeaderToolTip constructor propagates the parameters to the QToolTip constructor. void HeaderToolTip::maybeTip ( const QPoint& p ) { QHeader *header = (QHeader*)parentWidget(); int section = 0; The implementation of maybeTip() uses the QHeader API to get the section at the requested position and uses QToolTip::tip() to display the section's label in a tooltip. The second string is used by QToolTipGroup and will show up in the statusbar.
TableToolTip::TableToolTip( QTable *tipTable, QToolTipGroup *group ) Since QTable is a QScrollView all user interaction happens on QTable's viewport() . The TableToolTip constructor passes the viewport() and the tooltip group to the QToolTip constructor, and initializes the table member with the QTable pointer itself.
The implementation of maybeTip() uses the QTable API to get information about the cell at the requested position. The QTable API expects contents coordinates, and since the requested point is relative to the viewport we need to translate the coordinates before we can use QTable's functions.
We translate the cell's geometry back to viewport coordinates so that the tooltip disappears when the mouse cursor leaves the cell, and use QToolTip::tip() to display the cell's label in a tooltip and to provide text for the QToolTipGroup as before. class WhatsThis : public QObject, public QWhatsThis { Q_OBJECT public: WhatsThis( QWidget *w, QWidget *watch = 0 ); bool clicked( const QString &link ); QWidget *parentWidget() const; signals: void linkClicked( const QString &link ); private: QWidget *widget; }; The WhatsThis class is a subclass of both QObject and
QWhatsThis and serves as a base class for the HeaderWhatsThis
and TableWhatsThis classes. (1) The WhatsThis constructor takes two parameters, the first is the
widget we want to provide WhatsThis for, and the second is the
one which receives the events. Normally this is the same widget,
but some widgets, like QTable, are more complex and have a
viewport() widget which receives the events. If such a widget
is passed to the constructor it will propagate the parameter to
the QWhatsThis constructor and store the QWidget pointer itself
in it's member variable to allow easier use of the QWidget API
later on.
The implementation of clicked() emits the linkClicked() signal
if a hyperlink has been clicked.
The HeaderWhatsThis and TableWhatsThis classes reimplement
text() to make it possible to return texts depending on the
mouse click's position. All the other functionality is
already provided by the generic WhatsThis base class. We ensure
type safety here in the same manner as in the tooltip classes.
The HeaderWhatsThis constructor propagates the parameter to the
WhatsThis constructor.
The implementation of text() uses the QHeader API to determine
whether we have a horizontal or a vertical header and returns
a string which states the header's orientation and section.
(2) Since QTable is a scrollview and has a viewport() which receives
the events, we propagate the table itself and the table's
viewport() to the WhatsThis constructor.
The implementation of text() uses the QTable API to get
information about the cell at the requested position.
The QTable API expects contents coordinates, so we need to
translate the point as shown earlier for the tooltip classes.
We use the rtti() function to figure out the item's type
and return a string accordingly.
A QMainWindow is used to create a user interface that uses the
above classes in addition to Qt Assistant to provide context
sensitive help in the application.
The MainWindow class declares a slot called assistantSlot()
which creates an instance of Qt Assistant when it is called.
The class keeps references to the tooltip classes as members
because they are not QObjects and need to be deleted explicitly.
The class has a reference to QAssistantClient as a
member as well, to allow easier access to Qt Assistant later on.
The MainWindow constructor creates an instance of
QAssistantClient using QString::null as the first argument
so that the system path is used.
A QTable is used as the central widget and the table, the menus
and the toolbar are populated.
The static function whatsThisButton() creates a QToolButton
which will enter "What's this?" mode when clicked.
A QToolTipGroup is created and will show and remove tooltips
in the statusbar as the tooltips are displayed on the widgets.
The tooltips are set up. The static function add() sets up a
tooltip on the Assistant toolbutton. Tooltip objects are created
using the QToolTip subclasses, the constructor's first parameter
specifies the widget we want to add dynamic tooltips for and the
second argument specifies the QToolTipGroup they should belong
to.
The WhatsThis help is set up. The static function add() adds
What's This? help for the toolbutton which opens Assistant.
Instances of the two WhatsThis subclasses are created for the
headers and the table. What's This? help is also added for the
menu items.
Signals and slots are connected, so that the relevant pages will
be displayed in Qt Assistant when clicking on a hyperlink or on
the assistant button.
The destructor deletes the tooltips. We need to delete the
tooltips explicitly since QToolTip is, as mentioned above, not
a subclass of QObject and the instances of QToolTip not will be
deleted when the widget is deleted.
The assistantSlot() uses applicationDirPath() to find the
location of the documentation files and shows the specified page
in Qt Assistant.
The main function is a standard implementation opening
the application main window.
To build the example go to the helpsystem directory
(QTDIR/examples/helpsystem) run qmake to generate the makefile,
and use the make tool to build the library.
|
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.2 | |
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