博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt浅谈之右下角浮出界面
阅读量:4179 次
发布时间:2019-05-26

本文共 3108 字,大约阅读时间需要 10 分钟。

一、简介

 

       csdn博客上看到了一个比较简单的动画,类似windows下右下角的弹出广告界面。故记录在此,以便查阅。

 

二、详解

1、部分代码

(1)rightpop.h

#ifndef RIGHTPOP_H#define RIGHTPOP_H#include "epushbutton.h"#include 
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:。

你可能感兴趣的文章
Java并发| CountDownLatch、Semaphore和CyclicBarrier
查看>>
Spring的BeanUtils的copyProperties方法使用注意事项
查看>>
JAVA8 stream
查看>>
SpringBoot | 异步编程
查看>>
Java并发| Exchanger和Phaser
查看>>
IDEA RESTful Client 数据请求
查看>>
轻松读懂字节码文件
查看>>
记一下对String#intern的理解
查看>>
Tomcat多种方式部署多个项目
查看>>
随机字符串
查看>>
ZipUtils工具类
查看>>
1071 - Specified key was too long; max key length is 767 bytes
查看>>
SQLSERVER的LOG文件过于庞大
查看>>
Mycat+SpringBoot完成分库分表
查看>>
基于JMH检验多种生成随机数方法的效率
查看>>
读写文件效率测试
查看>>
优雅停止 SpringBoot 服务
查看>>
轻松读懂WSDL文件
查看>>
FastJson中JSONPath的应用
查看>>
文件下载
查看>>