Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Fonctions  · 

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:

/****************************************************************************
** $Id:  qt/tooltip.h   3.0.6   edited Oct 12 2001 $
**
** 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 <qwidget.h>
#include <qtooltip.h>


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;
};


Implementation:

/****************************************************************************
** $Id:  qt/tooltip.cpp   3.0.6   edited Oct 12 2001 $
**
** 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 <qapplication.h>
#include <qpainter.h>
#include <stdlib.h>


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 );
}


Main:

/****************************************************************************
** $Id:  qt/main.cpp   3.0.6   edited Oct 12 2001 $
**
** 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 <qapplication.h>
#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();
}

See also Examples.

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.0
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 !
 
 
 
 
Partenaires

Hébergement Web