Qt浅谈之右下角浮出界面
2016-01-29 15:56
555 查看
一、简介
csdn博客上看到了一个比较简单的动画,类似windows下右下角的弹出广告界面。故记录在此,以便查阅。
二、详解
1、部分代码
(1)rightpop.h#ifndef RIGHTPOP_H #define RIGHTPOP_H #include "epushbutton.h" #include <QtGui> class RightPop : public QWidget { Q_OBJECT public: RightPop(QWidget *parent = 0); ~RightPop(); void showMessage(); protected: void paintEvent(QPaintEvent *event); private slots: void onMove(); void onStay(); void onClose(); void onExit(); private: QPixmap backGroundPix; EPushButton *closeButton; QTimer *showTimer; QTimer *stayTimer; QTimer *closeTimer; QPoint point; double transparentPercent; int desktopHeight; }; #endif // RIGHTPOP_H(2)rightpop.cpp
#include "rightpop.h" RightPop::RightPop(QWidget *parent) : QWidget(parent, Qt::FramelessWindowHint| Qt::ToolTip) , transparentPercent(1.0) { resize(300, 200); backGroundPix.load(":/background.png"); backGroundPix = backGroundPix.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); closeButton = new EPushButton(this); closeButton->setPixName(":/close"); closeButton->setToolTip(tr("close")); closeButton->move(width() - 27, 0); connect(closeButton, SIGNAL(clicked()), this, SLOT(onExit())); showTimer = new QTimer(this); showTimer->setInterval(5); stayTimer = new QTimer(this); stayTimer->setInterval(5000); closeTimer = new QTimer(this); closeTimer->setInterval(5); connect(showTimer, SIGNAL(timeout()), this, SLOT(onMove())); connect(stayTimer, SIGNAL(timeout()), this, SLOT(onStay())); connect(closeTimer, SIGNAL(timeout()), this, SLOT(onClose())); showMessage(); } RightPop::~RightPop() { } void RightPop::showMessage() { QRect rect = QApplication::desktop()->availableGeometry(); point.setX(rect.width() - width()); point.setY(rect.height() + 50); desktopHeight = rect.height() + 50; move(point.x(), point.y()); showTimer->start(); } void RightPop::onMove() { desktopHeight--; move(point.x(), desktopHeight); if (desktopHeight <= point.y() - 200) { showTimer->stop(); stayTimer->start(); } } void RightPop::onStay() { stayTimer->stop(); closeTimer->start(); } void RightPop::onClose() { // transparentPercent -= 0.1; // qDebug() << transparentPercent; // if (transparentPercent <= 0.0) { // closeTimer->stop(); // onExit(); // } // else { // setWindowOpacity(transparentPercent); // } desktopHeight++; move(point.x(), desktopHeight); if (desktopHeight >= point.y()) { closeTimer->stop(); onExit(); } } void RightPop::onExit() { exit(0); } void RightPop::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.drawPixmap(0, 0, width(), height(), backGroundPix); painter.setFont(QFont("arial", 10, QFont::Bold)); painter.setPen(QColor("#FFFFFF")); painter.setBrush(QColor("#FFFFFF")); painter.drawText(QRectF(5, 5, 100, 35), tr("Happy New Year")); painter.drawRect(QRectF(0, 30, width(), height() - 30)); QWidget::paintEvent(event); }
三、总结
(1)在centos下setWindowOpacity(0)设置透明度没有效果,故采用弹入弹出界面。(2)上述代码采用定时器的启动和关闭来简单实现动画的功能。
(3)完整的工程代码已上传到CSDN:http://download.csdn.net/detail/taiyang1987912/9422290。
相关文章推荐
- Debian8.1-x64下安装Qt5.5.1+sip4.17+QScintilla-gpl-2.9.1+PyQt-gpl-5.5.1+eric6-6.1.1
- 基于Qt5图形视图框架的“俄罗斯方块”
- Qt调用摄像头(截取并保存图片)
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”
- QT5 添加 newtwork 库后还是无法找到库(LNK2019)的解决方法
- Qt5 多显示器获取不同显示器的分辨率和位置的方法
- QT create快捷键
- qt qml中PropertyAnimation的几种使用方法
- QTP知识点总结
- Using the Qt Creator IDE with non Qt projects
- how to get current path in qt
- how to add external library in qt under ubuntu
- INSTALL CGAL on ubuntu and use it in qt
- QT发展简史
- 再谈QVector与QByteArray——Qt的写时复制(copy on write)技术
- 自学QT之QWidget::paintEngine: Should no longer be called的解决办法
- PyQt学习笔记02-drag & drop
- 自学QT之QMovie播放gif(播放、暂停和继续)
- QT5+OpenCV美白效果
- Qt之进程间通信(QProcess)