聊天软件中右下角窗口上滑提示有好友上线,窗口下滑提示有好友下线。
在 Qt 下实现此功能,用到的类有 QPoint QTimer
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QPoint> 6 #include <QTimer> 7 8 namespace Ui { 9 class MainWindow; 10 } 11 12 class MainWindow : public QMainWindow 13 { 14 Q_OBJECT 15 16 public: 17 explicit MainWindow(QWidget *parent = 0); 18 void point_to_point(); 19 ~MainWindow(); 20 21 private: 22 Ui::MainWindow *ui; 23 QTimer* M_Timer; 24 QTimer* M_Timer1; 25 QTimer* M_Timer2; 26 int Counter; 27 QPoint curPos; 28 29 private slots: 30 void timerDone(); 31 void timerDone1(); 32 void timerDone2(); 33 }; 34 35 #endif // MAINWINDOW_H
mainwindow.cpp
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) : 5 QMainWindow(parent), 6 ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 M_Timer = new QTimer(); 10 M_Timer1 = new QTimer(); 11 M_Timer2 = new QTimer(); 12 this->move(950,768); 13 curPos=this->pos(); 14 connect(M_Timer, SIGNAL(timeout()),this, SLOT(timerDone())); 15 connect(M_Timer1, SIGNAL(timeout()),this, SLOT(timerDone1())); 16 connect(M_Timer2, SIGNAL(timeout()),this, SLOT(timerDone2())); 17 point_to_point(); 18 } 19 20 MainWindow::~MainWindow() 21 { 22 delete ui; 23 } 24 25 void MainWindow :: point_to_point() 26 { 27 M_Timer->start(100); 28 curPos=this->pos(); 29 QPoint TmpPos(curPos.x(),curPos.y()-10); 30 this->move(TmpPos); 31 } 32 33 void MainWindow :: timerDone() 34 { 35 curPos=this->pos(); 36 QPoint TmpPos(curPos.x(),curPos.y()-10); 37 this->move(TmpPos); 38 Counter = curPos.y(); 39 qDebug() << curPos.y(); 40 if(Counter < 500) 41 { 42 M_Timer->stop(); 43 M_Timer2->start(1000); 44 } 45 } 46 47 void MainWindow :: timerDone1() 48 { 49 curPos=this->pos(); 50 QPoint TmpPos(curPos.x(),curPos.y()+10); 51 this->move(TmpPos); 52 Counter = curPos.y(); 53 if(Counter > 660) 54 { 55 M_Timer1->stop(); 56 } 57 } 58 59 void MainWindow :: timerDone2() 60 { 61 curPos=this->pos(); 62 QPoint TmpPos(curPos.x(),curPos.y()); 63 this->move(TmpPos); 64 M_Timer2->stop(); 65 M_Timer1->start(100); 66 }
2014-07-28 21:03:08