Qt实现冒泡提示框

通过QLabel创建类似冒泡方式的提示框(提示框显示位置为父类控件居中位置,具体可根据需要自行修改),鼠标停留提示框界面时查看信息,离开时自动淡化消失的效果;

头文件定义

#ifndef _TTipWidget_H_
#define _TTipWidget_H_

class TTipWidget :public QLabel
{
    Q_OBJECT

public:
    static TTipWidget &Instance();
    void SetMesseage(const QString &strMessage, const QPoint *pPoint=nullptr);

private:
    TTipWidget();
    ~TTipWidget();

    void OnTimer();
    void enterEvent(QEvent *);
    void leaveEvent(QEvent *);

private:
    QWidget *mpParent;
    QTimer  *mpTimer;
    bool     mbEnter;
    int      mnTransparent;
};

#endif // _TTipWidget_H_

实例实现

#include "TTipWidget.h"

TTipWidget::TTipWidget()
    : mpParent(nullptr)
    , mbEnter(false)
    , mnTransparent(200)
{
    setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setAlignment(Qt::AlignCenter);

    mpTimer = new QTimer(this);
    connect(mpTimer, &QTimer::timeout, this, &TTipWidget::OnTimer);
}

TTipWidget::~TTipWidget()
{
    deleteLater();
}

void TTipWidget::enterEvent(QEvent *)
{
    mbEnter       = true;
    mnTransparent = 200;
    setStyleSheet(QString("color:white;font:12px \"Microsoft YaHei\";border-radius:5px;background-color:rgba(80, 80, 80, %1);").arg(mnTransparent));
}

void TTipWidget::leaveEvent(QEvent *)
{
    mbEnter = false;
}

void TTipWidget::OnTimer()
{
    if (mbEnter)
    {
        return;
    }

    mnTransparent -= 3;
    if (mnTransparent > 0)
    {
        if (mpParent && parentWidget())
        {
            QPoint pt((parentWidget()->width() - width()) >> 1, (parentWidget()->height() - height()) >> 1);
            if (pos() != pt)
            {
                move(pt);
            }
        }
        setStyleSheet(QString("color:white;font:12px \"Microsoft YaHei\";border-radius:5px;background-color:rgba(80, 80, 80, %1);").arg(mnTransparent));
    }
    else
    {
        mpTimer->stop();
        setVisible(false);
    }
}

void TTipWidget::SetMesseage(const QString &strMessage, const QPoint *pPoint)
{
    if (strMessage.isEmpty())
    {
        return;
    }
        
    QFontMetrics fm1(font());
    setFixedSize(fm1.width(strMessage) + 30, 30);

    mpParent = parentWidget();

    if (width() > mpParent->width())
    {
        setFixedSize(mpParent->width() - 60, 60);
        setWordWrap(true);
    }
    else
    {
        setWordWrap(false);
    }

    setText(strMessage);

    if (nullptr != mpParent)
    {
        if (nullptr != pPoint)
        {
            move(mpParent->mapFromGlobal(*pPoint));
            mpParent = nullptr;
        }
        else
        {
            move((mpParent->width() - width()) >> 1, (mpParent->height() - height()) >> 1);
        }
    }

    setVisible(true);
    mnTransparent = 200;
    
    mpTimer->start(30);
}

TTipWidget &TTipWidget::Instance()
{
    static TTipWidget tipWidget;
    return tipWidget;
}

外部引用

TTipWidget::Instance().setParent(this);   //设置实例的引用者
TTipWidget::Instance().setVisible(false);

TTipWidget::Instance().SetMesseage(tr("Disappearing tips!"));  //提示信息

 

posted @ 2017-07-12 15:26  sz_leez  阅读(4191)  评论(3编辑  收藏  举报