Qt tool tips高级用法

该示例展示了如何在Qt的Widget中使用动态和静态工具提示。程序创建了一个包含三个矩形的窗口,两个蓝色矩形会在点击后移动,带有动态生成的工具提示,而红色矩形保持不动,显示静态工具提示。动态提示基于鼠标位置更新信息,而静态提示则固定不变。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载:   

天天进步2015

此示例演示了如何在Widget中使用静态和动态区域的工具提示。

它显示两个蓝色和一个红色矩形。 每次单击时,蓝色的都会移动,红色的是静止的。 蓝色矩形上有动态工具提示,红色矩形上有静态工具提示。

Header file:
 
/****************************************************************************
** $Id: qt/tooltip.h   3.1.2   edited Nov 8 10:35 $
**
** 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.1.2   edited Nov 8 10:35 $
**
** 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.1.2   edited Nov 8 10:35 $
**
** 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();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值