Qt——屏保小球

dialog.h:

#ifndef DIALOG_H
#define DIALOG_H
 
#include <QDialog>
#include<QTimerEvent>
#include<QLabel>
#include<QMouseEvent>
 
namespace Ui {
class Dialog;
}
 
class Dialog : public QDialog
{
    Q_OBJECT
 
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
 
private:
    Ui::Dialog *ui;
    int x,y;
    int dx,dy;
protected:
    void mousePressEvent(QMouseEvent *e);
    void timerEvent(QTimerEvent *event);
    int mclock;
};
 
#endif // DIALOG_H
 

dialog.cpp:

#include "dialog.h"
#include "ui_dialog.h"
 
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    dx=dy=1;
}
 
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton)
    {
        mclock=this->startTimer(20);
    }
    else if(e->button()==Qt::RightButton)
    {
        this->killTimer(mclock);
    }
}
 void  Dialog::timerEvent(QTimerEvent *event)
 {
 
     x=ui->ball->x();
     y=ui->ball->y();
     if(x==800) dx=0;
     else if(x==0) dx=1;
 
     if(y==600) dy=0;
     else if(y==0) dy=1;
 
     if(dx)x+=10;
     else x-=10;
 
     if(dy)y+=10;
     else y-=10;
 
     ui->ball->move(x,y);
 }
 
扫描二维码关注公众号,回复: 2820974 查看本文章

猜你喜欢

转载自blog.csdn.net/lannister_awalys_pay/article/details/81299402