Qt——屏保小球

Qt——屏保小球_第1张图片

dialog.h:

#ifndef DIALOG_H
#define DIALOG_H
 
#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;
    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);
 }
 

 

 

 

 

你可能感兴趣的:(QT图形化界面,QT图形化界面设计)