From bd0f3345a938b35ce6a12f6150373b0955b8dd12 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sun, 10 Jul 2011 15:24:15 -0500 Subject: Add Qt3 development HEAD version --- examples/tooltip/README | 8 +++ examples/tooltip/main.cpp | 23 +++++++++ examples/tooltip/tooltip.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++ examples/tooltip/tooltip.doc | 33 ++++++++++++ examples/tooltip/tooltip.h | 43 ++++++++++++++++ examples/tooltip/tooltip.pro | 11 ++++ 6 files changed, 234 insertions(+) create mode 100644 examples/tooltip/README create mode 100644 examples/tooltip/main.cpp create mode 100644 examples/tooltip/tooltip.cpp create mode 100644 examples/tooltip/tooltip.doc create mode 100644 examples/tooltip/tooltip.h create mode 100644 examples/tooltip/tooltip.pro (limited to 'examples/tooltip') diff --git a/examples/tooltip/README b/examples/tooltip/README new file mode 100644 index 0000000..cdfe50c --- /dev/null +++ b/examples/tooltip/README @@ -0,0 +1,8 @@ +The tooltip example demonstrates advanced use of tool tips. + +There are dynamic tool tips on the blue rectanges - QToolTip asks +tooltip whether to display anything, and if so, what. The Right Thing +happens when you click on the rectangles. + +There is also a static tool tip on the red rectangle. + diff --git a/examples/tooltip/main.cpp b/examples/tooltip/main.cpp new file mode 100644 index 0000000..778e874 --- /dev/null +++ b/examples/tooltip/main.cpp @@ -0,0 +1,23 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. 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 +#include "tooltip.h" + +int main( int argc, char ** argv ) +{ + QApplication a( argc, argv ); + + TellMe mw; + mw.setCaption( "Qt Example - Dynamic Tool Tips" ); + a.setMainWidget( &mw ); + mw.show(); + + return a.exec(); +} diff --git a/examples/tooltip/tooltip.cpp b/examples/tooltip/tooltip.cpp new file mode 100644 index 0000000..0d08de0 --- /dev/null +++ b/examples/tooltip/tooltip.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. 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 +#include +#include + + +DynamicTip::DynamicTip( QWidget * parent ) + : QToolTip( parent ) +{ + // no explicit initialization needed +} + + +void DynamicTip::maybeTip( const QPoint &pos ) +{ + if ( !parentWidget()->inherits( "TellMe" ) ) + return; + + QRect r( ((TellMe*)parentWidget())->tip(pos) ); + if ( !r.isValid() ) + return; + + QString s; + s.sprintf( "position: %d,%d", r.center().x(), r.center().y() ); + tip( r, s ); +} + + +TellMe::TellMe( QWidget * parent , const char * name ) + : QWidget( parent, name ) +{ + setMinimumSize( 30, 30 ); + r1 = randomRect(); + r2 = randomRect(); + r3 = randomRect(); + + t = new DynamicTip( this ); + + QToolTip::add( this, r3, "this color is called red" ); // <- helpful +} + + +TellMe::~TellMe() +{ + delete t; + t = 0; +} + + +void TellMe::paintEvent( QPaintEvent * e ) +{ + QPainter p( this ); + + // I try to be efficient here, and repaint only what's needed + + if ( e->rect().intersects( r1 ) ) { + p.setBrush( blue ); + p.drawRect( r1 ); + } + + if ( e->rect().intersects( r2 ) ) { + p.setBrush( blue ); + p.drawRect( r2 ); + } + + if ( e->rect().intersects( r3 ) ) { + p.setBrush( red ); + p.drawRect( r3 ); + } +} + + +void TellMe::mousePressEvent( QMouseEvent * e ) +{ + if ( r1.contains( e->pos() ) ) + r1 = randomRect(); + if ( r2.contains( e->pos() ) ) + r2 = randomRect(); + repaint(); +} + + +void TellMe::resizeEvent( QResizeEvent * ) +{ + if ( !rect().contains( r1 ) ) + r1 = randomRect(); + if ( !rect().contains( r2 ) ) + r2 = randomRect(); +} + + +QRect TellMe::randomRect() +{ + return QRect( ::rand() % (width() - 20), ::rand() % (height() - 20), + 20, 20 ); +} + + +QRect 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 ); +} diff --git a/examples/tooltip/tooltip.doc b/examples/tooltip/tooltip.doc new file mode 100644 index 0000000..41800ff --- /dev/null +++ b/examples/tooltip/tooltip.doc @@ -0,0 +1,33 @@ +/* +*/ +/*! \page tooltip-example.html + + \ingroup examples + \title Advanced use of tool tips + + 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. + +
+ + Header file: + + \include tooltip/tooltip.h + +
+ + Implementation: + + \include tooltip/tooltip.cpp + +
+ + Main: + + \include tooltip/main.cpp +*/ + diff --git a/examples/tooltip/tooltip.h b/examples/tooltip/tooltip.h new file mode 100644 index 0000000..735d22d --- /dev/null +++ b/examples/tooltip/tooltip.h @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. 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 +#include + + +class DynamicTip : public QToolTip +{ +public: + DynamicTip( QWidget * parent ); + +protected: + void maybeTip( const QPoint & ); +}; + + +class TellMe : public QWidget +{ + Q_OBJECT +public: + TellMe( QWidget * parent = 0, const char * name = 0 ); + ~TellMe(); + + QRect tip( const QPoint & ); + +protected: + void paintEvent( QPaintEvent * ); + void mousePressEvent( QMouseEvent * ); + void resizeEvent( QResizeEvent * ); + +private: + QRect randomRect(); + + QRect r1, r2, r3; + DynamicTip * t; +}; diff --git a/examples/tooltip/tooltip.pro b/examples/tooltip/tooltip.pro new file mode 100644 index 0000000..d0c2fdb --- /dev/null +++ b/examples/tooltip/tooltip.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = tooltip + +CONFIG += qt warn_on release +DEPENDPATH = ../../include + +REQUIRES = large-config + +HEADERS = tooltip.h +SOURCES = main.cpp \ + tooltip.cpp -- cgit v1.2.1