2019独角兽企业重金招聘Python工程师标准>>>
使用Qt写的类QQ右下角弹出框
/***main.cpp
*/
#include <QtGui/QApplication>
#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 <QDialog>
#include <QDesktopWidget>
#include <QPropertyAnimation>
#include <QPoint>
#include <QTimer>namespace Ui {
class Dialog;
}class Dialog : public QDialog
{Q_OBJECTpublic: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 <QDebug>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;
}