Qt浅谈之右下角浮出界面

一、简介

 

       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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦2号

博文不易,支持的请给予小小打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值