tooltip-main-cpp.html
来自「qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人」· HTML 代码 · 共 227 行
HTML
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> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></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.8 edited 2004-05-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 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_OBJECTpublic: 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.8 edited 2004-05-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 "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="60"></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="61"></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="62"></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="63"></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="64"></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="65"></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.8 edited 2004-05-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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?