本文共 1919 字,大约阅读时间需要 6 分钟。
实现原理
#ifndef _QTOOLTIPS_#define _QTOOLTIPS_#include#include #include "ui_QToolTips.h"class QToolTips:public QDialog{ Q_OBJECTpublic: QToolTips(QWidget *parent = 0); ~QToolTips(); void showMessage(const char* str);private slots: void onMove(); void onStay();- void onClose();private: Ui::QToolTips ui; QTimer * m_pShowTimer; QTimer * m_pStayTimer; QTimer * m_pCloseTimer; QPoint m_point; int m_nDesktopHeight; double m_dTransparent;};#endif
#include "QToolTips.h"#include#include QToolTips::QToolTips(QWidget *parent /*= 0*/) : QDialog(parent){ setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint); ui.setupUi(this); m_nDesktopHeight = QApplication::desktop()->height(); m_dTransparent = 1.0; m_pShowTimer = new QTimer(this); m_pStayTimer = new QTimer(this); m_pCloseTimer = new QTimer(this); connect(m_pShowTimer, SIGNAL(timeout()), this, SLOT(onMove())); connect(m_pStayTimer, SIGNAL(timeout()), this, SLOT(onStay())); connect(m_pCloseTimer, SIGNAL(timeout()), this, SLOT(onClose()));}QToolTips::~QToolTips(){ }void QToolTips::showMessage(const char* str){ ui.m_label->setStyleSheet("background-color:rgb(255,210,200);font:60px;color:blue"); ui.m_label->setText(str); QRect rect = QApplication::desktop()->availableGeometry(); m_point.setX(rect.width() - width()); m_point.setY(rect.height() - height()); move(m_point.x(), m_point.y()); m_pShowTimer->start(5);}void QToolTips::onMove(){ m_nDesktopHeight--; move(m_point.x(), m_nDesktopHeight); if (m_nDesktopHeight <= m_point.y()) { m_pShowTimer->stop(); m_pStayTimer->start(5000); }}void QToolTips::onStay(){ m_pStayTimer->stop(); m_pCloseTimer->start(100);}void QToolTips::onClose(){ m_dTransparent -= 0.1; if (m_dTransparent <= 0.0) { m_pCloseTimer->stop(); close(); } else { setWindowOpacity(m_dTransparent); }}
转载地址:http://dvqxi.baihongyu.com/