1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>
Qt Toolkit - tooltip/main.cpp example file
</title><style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">
<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>
<h1 align=center>Advanced use of tool tips</h1><br clear="all">
This example widget demonstrates how to use tool tips for static and
dynamic regions within a widget.
It displays two blue and one red rectangle. The blue ones move every
time you click on them, the red one is static. There are dynamic
tool tips on the blue rectangles and a static tool tip on the red one.
<hr>
Header file: <pre>/****************************************************************************
** $Id: qt/examples/tooltip/tooltip.h 2.3.2 edited 2001-01-26 $
**
** 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 <<a href="qwidget-h.html">qwidget.h</a>>
#include <<a href="qtooltip-h.html">qtooltip.h</a>>
class DynamicTip : public QToolTip
{
public:
DynamicTip( <a href="qwidget.html">QWidget</a> * parent );
protected:
void maybeTip( const QPoint & );
};
class TellMe : public QWidget
{
Q_OBJECT
public:
TellMe( <a href="qwidget.html">QWidget</a> * parent = 0, const char * name = 0 );
~TellMe();
<a href="qrect.html">QRect</a> tip( const QPoint & );
protected:
void paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * );
void mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * );
void resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * );
private:
<a href="qrect.html">QRect</a> randomRect();
<a href="qrect.html">QRect</a> r1, r2, r3;
DynamicTip * t;
};
</pre>
<hr>
Implementation: <pre>/****************************************************************************
** $Id: qt/examples/tooltip/tooltip.cpp 2.3.2 edited 2001-01-26 $
**
** 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 "tooltip.h"
#include <<a href="qapplication-h.html">qapplication.h</a>>
#include <<a href="qpainter-h.html">qpainter.h</a>>
#include <stdlib.h>
DynamicTip::DynamicTip( <a href="qwidget.html">QWidget</a> * parent )
: <a href="qtooltip.html">QToolTip</a>( parent )
{
// no explicit initialization needed
}
void <a name="310"></a>DynamicTip::maybeTip( const QPoint &pos )
{
if ( !parentWidget()->inherits( "TellMe" ) )
return;
<a href="qrect.html">QRect</a> r( ((TellMe*)parentWidget())->tip(pos) );
if ( !r.<a href="qrect.html#6d4ece">isValid</a>() )
return;
<a href="qstring.html">QString</a> s;
s.<a href="qstring.html#926f67">sprintf</a>( "position: %d,%d", r.<a href="qrect.html#3daab9">center</a>().x(), r.<a href="qrect.html#3daab9">center</a>().y() );
<a href="qtooltip.html#585b31">tip</a>( r, s );
}
TellMe::TellMe( <a href="qwidget.html">QWidget</a> * parent , const char * name )
: <a href="qwidget.html">QWidget</a>( parent, name )
{
<a href="qwidget.html#c0b5fb">setMinimumSize</a>( 30, 30 );
r1 = randomRect();
r2 = randomRect();
r3 = randomRect();
t = new DynamicTip( this );
<a href="qtooltip.html#184bfe">QToolTip::add</a>( this, r3, "this color is called red" ); // <- helpful
}
TellMe::~TellMe()
{
delete t;
t = 0;
}
void <a name="311"></a>TellMe::paintEvent( <a href="qpaintevent.html">QPaintEvent</a> * e )
{
<a href="qpainter.html">QPainter</a> p( this );
// I try to be efficient here, and repaint only what's needed
if ( e->rect().intersects( r1 ) ) {
p.<a href="qpainter.html#3e0cc8">setBrush</a>( blue );
p.<a href="qpainter.html#4c0077">drawRect</a>( r1 );
}
if ( e->rect().intersects( r2 ) ) {
p.<a href="qpainter.html#3e0cc8">setBrush</a>( blue );
p.<a href="qpainter.html#4c0077">drawRect</a>( r2 );
}
if ( e->rect().intersects( r3 ) ) {
p.<a href="qpainter.html#3e0cc8">setBrush</a>( red );
p.<a href="qpainter.html#4c0077">drawRect</a>( r3 );
}
}
void <a name="312"></a>TellMe::mousePressEvent( <a href="qmouseevent.html">QMouseEvent</a> * e )
{
if ( r1.contains( e-><a href="qmouseevent.html#ac6f25">pos</a>() ) )
r1 = randomRect();
if ( r2.contains( e-><a href="qmouseevent.html#ac6f25">pos</a>() ) )
r2 = randomRect();
<a href="qwidget.html#7569b1">repaint</a>();
}
void <a name="313"></a>TellMe::resizeEvent( <a href="qresizeevent.html">QResizeEvent</a> * )
{
if ( !rect().contains( r1 ) )
r1 = randomRect();
if ( !rect().contains( r2 ) )
r2 = randomRect();
}
<a href="qrect.html">QRect</a> <a name="314"></a>TellMe::randomRect()
{
return QRect( ::rand() % (<a href="qwidget.html#2edab1">width</a>() - 20), ::rand() % (<a href="qwidget.html#e3c588">height</a>() - 20),
20, 20 );
}
<a href="qrect.html">QRect</a> <a name="315"></a>TellMe::tip( const QPoint & p )
{
if ( r1.contains( p ) )
return r1;
else if ( r2.contains( p ) )
return r2;
else
return QRect( 0,0, -1,-1 );
}
</pre>
<hr>
Main:
<pre>/****************************************************************************
** $Id: qt/examples/tooltip/main.cpp 2.3.2 edited 2001-06-12 $
**
** 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 <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>
#include "tooltip.h"
int main( int argc, char ** argv )
{
<a name="QApplication"></a><a href="qapplication.html">QApplication</a> a( argc, argv );
TellMe mw;
mw.<a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Example - Dynamic Tool Tips" );
a.<a name="setMainWidget"></a><a href="qapplication.html#7ad759">setMainWidget</a>( &mw );
mw.<a name="show"></a><a href="qwidget.html#200ee5">show</a>();
return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();
}
</pre>
<p><address><hr><div align="center">
<table width="100%" cellspacing="0" border="0"><tr>
<td>Copyright 2001 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.2</div>
</table></div></address></body></html>
|