1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/tooltip/tooltip.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Advanced use of tool tips</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Advanced use of tool tips</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example widget demonstrates how to use tool tips for static and
|
---|
37 | dynamic regions within a widget.
|
---|
38 | <p> It displays two blue and one red rectangle. The blue ones move every
|
---|
39 | time you click on them, the red one is static. There are dynamic
|
---|
40 | tool tips on the blue rectangles and a static tool tip on the red one.
|
---|
41 | <p> <hr>
|
---|
42 | <p> Header file:
|
---|
43 | <p> <pre>/****************************************************************************
|
---|
44 | ** $Id: tooltip-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
45 | **
|
---|
46 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
47 | **
|
---|
48 | ** This file is part of an example program for Qt. This example
|
---|
49 | ** program may be used, distributed and modified without limitation.
|
---|
50 | **
|
---|
51 | *****************************************************************************/
|
---|
52 |
|
---|
53 | #include <<a href="qwidget-h.html">qwidget.h</a>>
|
---|
54 | #include <<a href="qtooltip-h.html">qtooltip.h</a>>
|
---|
55 |
|
---|
56 |
|
---|
57 | class DynamicTip : public <a href="qtooltip.html">QToolTip</a>
|
---|
58 | {
|
---|
59 | public:
|
---|
60 | DynamicTip( <a href="qwidget.html">QWidget</a> * parent );
|
---|
61 |
|
---|
62 | protected:
|
---|
63 | void maybeTip( const <a href="qpoint.html">QPoint</a> & );
|
---|
64 | };
|
---|
65 |
|
---|
66 |
|
---|
67 | class TellMe : public <a href="qwidget.html">QWidget</a>
|
---|
68 | {
|
---|
69 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
70 | public:
|
---|
71 | TellMe( <a href="qwidget.html">QWidget</a> * parent = 0, const char * name = 0 );
|
---|
72 | ~TellMe();
|
---|
73 |
|
---|
74 | <a href="qrect.html">QRect</a> tip( const <a href="qpoint.html">QPoint</a> & );
|
---|
75 |
|
---|
76 | protected:
|
---|
77 | void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
|
---|
78 | void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
|
---|
79 | void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
|
---|
80 |
|
---|
81 | private:
|
---|
82 | <a href="qrect.html">QRect</a> randomRect();
|
---|
83 |
|
---|
84 | <a href="qrect.html">QRect</a> r1, r2, r3;
|
---|
85 | DynamicTip * t;
|
---|
86 | };
|
---|
87 | </pre>
|
---|
88 |
|
---|
89 | <p> <hr>
|
---|
90 | <p> Implementation:
|
---|
91 | <p> <pre>/****************************************************************************
|
---|
92 | ** $Id: tooltip-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
93 | **
|
---|
94 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
95 | **
|
---|
96 | ** This file is part of an example program for Qt. This example
|
---|
97 | ** program may be used, distributed and modified without limitation.
|
---|
98 | **
|
---|
99 | *****************************************************************************/
|
---|
100 |
|
---|
101 | #include "tooltip.h"
|
---|
102 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
103 | #include <<a href="qpainter-h.html">qpainter.h</a>>
|
---|
104 | #include <stdlib.h>
|
---|
105 |
|
---|
106 |
|
---|
107 | <a name="f251"></a>DynamicTip::DynamicTip( <a href="qwidget.html">QWidget</a> * parent )
|
---|
108 | : <a href="qtooltip.html">QToolTip</a>( parent )
|
---|
109 | {
|
---|
110 | // no explicit initialization needed
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | <a name="x451"></a>void DynamicTip::<a href="qtooltip.html#maybeTip">maybeTip</a>( const <a href="qpoint.html">QPoint</a> &pos )
|
---|
115 | {
|
---|
116 | if ( !parentWidget()->inherits( "TellMe" ) )
|
---|
117 | return;
|
---|
118 |
|
---|
119 | <a href="qrect.html">QRect</a> r( ((TellMe*)<a href="qtooltip.html#parentWidget">parentWidget</a>())->tip(pos) );
|
---|
120 | <a name="x448"></a> if ( !r.<a href="qrect.html#isValid">isValid</a>() )
|
---|
121 | return;
|
---|
122 |
|
---|
123 | <a href="qstring.html">QString</a> s;
|
---|
124 | <a name="x449"></a><a name="x447"></a> s.<a href="qstring.html#sprintf">sprintf</a>( "position: %d,%d", r.<a href="qrect.html#center">center</a>().x(), r.<a href="qrect.html#center">center</a>().y() );
|
---|
125 | tip( r, s );
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | <a name="f252"></a>TellMe::TellMe( <a href="qwidget.html">QWidget</a> * parent , const char * name )
|
---|
130 | : <a href="qwidget.html">QWidget</a>( parent, name )
|
---|
131 | {
|
---|
132 | <a href="qwidget.html#setMinimumSize">setMinimumSize</a>( 30, 30 );
|
---|
133 | r1 = randomRect();
|
---|
134 | r2 = randomRect();
|
---|
135 | r3 = randomRect();
|
---|
136 |
|
---|
137 | t = new DynamicTip( this );
|
---|
138 |
|
---|
139 | <a name="x450"></a> QToolTip::<a href="qtooltip.html#add">add</a>( this, r3, "this color is called red" ); // <- helpful
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | TellMe::~TellMe()
|
---|
144 | {
|
---|
145 | delete t;
|
---|
146 | t = 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | void TellMe::<a href="qwidget.html#paintEvent">paintEvent</a>( <a href="qpaintevent.html">QPaintEvent</a> * e )
|
---|
151 | {
|
---|
152 | <a href="qpainter.html">QPainter</a> p( this );
|
---|
153 |
|
---|
154 | // I try to be efficient here, and repaint only what's needed
|
---|
155 |
|
---|
156 | <a name="x446"></a> if ( e-><a href="qpaintevent.html#rect">rect</a>().intersects( r1 ) ) {
|
---|
157 | <a name="x445"></a> p.<a href="qpainter.html#setBrush">setBrush</a>( blue );
|
---|
158 | <a name="x444"></a> p.<a href="qpainter.html#drawRect">drawRect</a>( r1 );
|
---|
159 | }
|
---|
160 |
|
---|
161 | if ( e-><a href="qpaintevent.html#rect">rect</a>().intersects( r2 ) ) {
|
---|
162 | p.<a href="qpainter.html#setBrush">setBrush</a>( blue );
|
---|
163 | p.<a href="qpainter.html#drawRect">drawRect</a>( r2 );
|
---|
164 | }
|
---|
165 |
|
---|
166 | if ( e-><a href="qpaintevent.html#rect">rect</a>().intersects( r3 ) ) {
|
---|
167 | p.<a href="qpainter.html#setBrush">setBrush</a>( red );
|
---|
168 | p.<a href="qpainter.html#drawRect">drawRect</a>( r3 );
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | <a name="x452"></a>void TellMe::<a href="qwidget.html#mousePressEvent">mousePressEvent</a>( <a href="qmouseevent.html">QMouseEvent</a> * e )
|
---|
174 | {
|
---|
175 | <a name="x443"></a> if ( r1.contains( e-><a href="qmouseevent.html#pos">pos</a>() ) )
|
---|
176 | r1 = randomRect();
|
---|
177 | if ( r2.contains( e-><a href="qmouseevent.html#pos">pos</a>() ) )
|
---|
178 | r2 = randomRect();
|
---|
179 | <a href="qwidget.html#repaint">repaint</a>();
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | <a name="x454"></a>void TellMe::<a href="qwidget.html#resizeEvent">resizeEvent</a>( <a href="qresizeevent.html">QResizeEvent</a> * )
|
---|
184 | {
|
---|
185 | if ( !rect().contains( r1 ) )
|
---|
186 | r1 = randomRect();
|
---|
187 | if ( !rect().contains( r2 ) )
|
---|
188 | r2 = randomRect();
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | QRect <a name="f253"></a>TellMe::randomRect()
|
---|
193 | {
|
---|
194 | return QRect( ::rand() % (<a href="qwidget.html#width">width</a>() - 20), ::rand() % (<a href="qwidget.html#height">height</a>() - 20),
|
---|
195 | 20, 20 );
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | QRect <a name="f254"></a>TellMe::tip( const <a href="qpoint.html">QPoint</a> & p )
|
---|
200 | {
|
---|
201 | if ( r1.contains( p ) )
|
---|
202 | return r1;
|
---|
203 | else if ( r2.contains( p ) )
|
---|
204 | return r2;
|
---|
205 | else
|
---|
206 | return QRect( 0,0, -1,-1 );
|
---|
207 | }
|
---|
208 | </pre>
|
---|
209 |
|
---|
210 | <p> <hr>
|
---|
211 | <p> Main:
|
---|
212 | <p> <pre>/****************************************************************************
|
---|
213 | ** $Id: tooltip-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
214 | **
|
---|
215 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
216 | **
|
---|
217 | ** This file is part of an example program for Qt. This example
|
---|
218 | ** program may be used, distributed and modified without limitation.
|
---|
219 | **
|
---|
220 | *****************************************************************************/
|
---|
221 |
|
---|
222 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
223 | #include "tooltip.h"
|
---|
224 |
|
---|
225 | int main( int argc, char ** argv )
|
---|
226 | {
|
---|
227 | <a href="qapplication.html">QApplication</a> a( argc, argv );
|
---|
228 |
|
---|
229 | TellMe mw;
|
---|
230 | mw.<a href="qwidget.html#setCaption">setCaption</a>( "Qt Example - Dynamic Tool Tips" );
|
---|
231 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &mw );
|
---|
232 | mw.<a href="qwidget.html#show">show</a>();
|
---|
233 |
|
---|
234 | return a.<a href="qapplication.html#exec">exec</a>();
|
---|
235 | }
|
---|
236 | </pre>
|
---|
237 |
|
---|
238 | <p>See also <a href="examples.html">Examples</a>.
|
---|
239 |
|
---|
240 | <!-- eof -->
|
---|
241 | <p><address><hr><div align=center>
|
---|
242 | <table width=100% cellspacing=0 border=0><tr>
|
---|
243 | <td>Copyright © 2007
|
---|
244 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
245 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
246 | </table></div></address></body>
|
---|
247 | </html>
|
---|