本文主要是介绍Qt浅谈之右下角浮出界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、简介
csdn博客上看到了一个比较简单的动画,类似windows下右下角的弹出广告界面。故记录在此,以便查阅。
二、详解
1、部分代码
(1)rightpop.h
#ifndef RIGHTPOP_H
#define RIGHTPOP_H#include "epushbutton.h"
#include <QtGui>class RightPop : public QWidget
{Q_OBJECTpublic: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。
这篇关于Qt浅谈之右下角浮出界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!