Qt做右下角彈出框
阿新 • • 發佈:2019-02-09
Qt的右下角彈出框、歡迎討論
本例子主要使用QPropertyAnimation作為動畫類。
直接上程式碼、註釋應該很清楚了、
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_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 <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; }