Qt做右下角弹出框

Qt的右下角弹出框、欢迎讨论

本例子主要使用QPropertyAnimation作为动画类。



直接上代码、注释应该很清楚了、

源代码下载地址:http://download.csdn.net/detail/silencesu/4583309 


main.cpp

#include 
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    
    return a.exec();
}

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT
    
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    
private:
    Ui::Dialog *ui;
    QDesktopWidget desktop;
    QPropertyAnimation* animation;
    QTimer *remainTimer;

    void showAnimation();
private slots:
    void closeAnimation();
    void clearAll();
};

#endif // DIALOG_H

dialog.cpp


#include "dialog.h"
#include "ui_dialog.h"
#include 

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint); //隐藏菜单栏
    this->move((desktop.availableGeometry().width()-this->width()),desktop.availableGeometry().height());//初始化位置到右下角
    showAnimation(); //开始显示右下角弹出框
}

Dialog::~Dialog()
{
    delete ui;
}
//弹出动画
void Dialog::showAnimation(){
    //显示弹出框动画
    animation=new QPropertyAnimation(this,"pos");
    animation->setDuration(2000);
    animation->setStartValue(QPoint(this->x(),this->y()));
    animation->setEndValue(QPoint((desktop.availableGeometry().width()-this->width()),(desktop.availableGeometry().height()-this->height())));
    animation->start();

    //设置弹出框显示2秒、在弹回去
    remainTimer=new QTimer();
    connect(remainTimer,SIGNAL(timeout()),this,SLOT(closeAnimation()));
    remainTimer->start(4000);//弹出动画2S,停留2S回去
}
//关闭动画
void Dialog::closeAnimation(){
    //清除Timer指针和信号槽
    remainTimer->stop();
    disconnect(remainTimer,SIGNAL(timeout()),this,SLOT(closeAnimation()));
    delete remainTimer;
    remainTimer=NULL;
    //弹出框回去动画
    animation->setStartValue(QPoint(this->x(),this->y()));
    animation->setEndValue(QPoint((desktop.availableGeometry().width()-this->width()),desktop.availableGeometry().height()));
    animation->start();
    //弹回动画完成后清理动画指针
    connect(animation,SIGNAL(finished()),this,SLOT(clearAll()));
}
//清理动画指针
void Dialog::clearAll(){
    disconnect(animation,SIGNAL(finished()),this,SLOT(clearAll()));
    delete animation;
    animation=NULL;
}


你可能感兴趣的:(Linux,Qt)