#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
{
Q_OBJECT
public:
~Widget();
//QWidget *widget1;
//QTimer *timer;
int x;
int x2;
int y;
int y2;
int dx, dy;
int dx2, dy2;
public slots:
void move();
};
#endif // WIDGET_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QGridLayout *grid;
//QWidget *widget1;
QPen *pen;
QBrush *brush, *brush1;
//QTimer *timer;
QMatrix matrix;
void paintEvent(QPaintEvent *);
int x;
int x2;
int y;
int y2;
int dx, dy;
int dx2, dy2;
void timerEvent(QTimerEvent *);
void timerEvent1(QTimerEvent *);
public slots:
void move();
};
#endif // WIDGET_H
To copy to clipboard, switch view to plain text mode
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
Widget w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include "widget.h"
{
//widget1= new QWidget;
//grid->addWidget(widget1,0,6,0,6);
this->setLayout(grid);
//pen->setColor(Qt::blue);
brush->setColor(Qt::red);
//timer = new QTimer(this);
/*x = 10;
dx = 5;
dy = 10;
y =30;*/
x = 10;
dx = 5;
dy = 10;
y =30;
x2 = 10;
dx2 = 5;
dy2 = 10;
y2 =30;
//connect(timer, SIGNAL(timeout()), this,SLOT(move()));
//timer->start(3000);
this->startTimer(100);
}
x = x + dx;
if(x >=165) dx = -5;
if(x <0) dx = 5;
y = y + dy;
if(y <=-165) dy = 10;
if(y >0) dy = -10;
// 2nd leg for blue ball
x2 = x2+ dx2;
if(x2 >=165) dx2 = 5;
if(x2 <0) dx2 = -5;
if(x2>5) dx2=1;
y2 = y2- dy2;
if(y2 <=-165) dy2 = 10;
if(y2 >0) dy2 = -10;
if(y2>-10) dx2=15;
/*if(y2>=100) dy2=5;
if(y2>0) dy2=-5;*/
/* y2 = y2 + dy2;
if(y2 <=100) dy2 = 1;
if(y2 >0) dy2 = -1;*/
update();
}
Widget::~Widget()
{
}
{
painter.setBrush(Qt::red);
/*QRadialGradient radialGradient(50, 50, 50, 70, 70);
radialGradient.setColorAt(0.0, Qt::white);
radialGradient.setColorAt(0.2, Qt::green);
radialGradient.setColorAt(1.0, Qt::black);
painter.setBrush(radialGradient);*/
painter.drawEllipse(x,y,40,40);
painter.setBrush(Qt::blue);
painter.drawEllipse(x2,y2,10,10);
}
void Widget::move()
{
painter.drawEllipse(0,0,165,165);
}
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
grid = new QGridLayout;
QPainter painter(this);
//widget1= new QWidget;
//grid->addWidget(widget1,0,6,0,6);
this->setLayout(grid);
pen = new QPen;
brush = new QBrush;
//pen->setColor(Qt::blue);
brush->setColor(Qt::red);
//timer = new QTimer(this);
/*x = 10;
dx = 5;
dy = 10;
y =30;*/
x = 10;
dx = 5;
dy = 10;
y =30;
x2 = 10;
dx2 = 5;
dy2 = 10;
y2 =30;
//connect(timer, SIGNAL(timeout()), this,SLOT(move()));
//timer->start(3000);
this->startTimer(100);
}
void Widget::timerEvent(QTimerEvent *){
x = x + dx;
if(x >=165) dx = -5;
if(x <0) dx = 5;
y = y + dy;
if(y <=-165) dy = 10;
if(y >0) dy = -10;
// 2nd leg for blue ball
x2 = x2+ dx2;
if(x2 >=165) dx2 = 5;
if(x2 <0) dx2 = -5;
if(x2>5) dx2=1;
y2 = y2- dy2;
if(y2 <=-165) dy2 = 10;
if(y2 >0) dy2 = -10;
if(y2>-10) dx2=15;
/*if(y2>=100) dy2=5;
if(y2>0) dy2=-5;*/
/* y2 = y2 + dy2;
if(y2 <=100) dy2 = 1;
if(y2 >0) dy2 = -1;*/
update();
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter (this);
painter.setBrush(Qt::red);
/*QRadialGradient radialGradient(50, 50, 50, 70, 70);
radialGradient.setColorAt(0.0, Qt::white);
radialGradient.setColorAt(0.2, Qt::green);
radialGradient.setColorAt(1.0, Qt::black);
painter.setBrush(radialGradient);*/
painter.drawEllipse(x,y,40,40);
painter.setBrush(Qt::blue);
painter.drawEllipse(x2,y2,10,10);
}
void Widget::move()
{
QPainter painter (this);
painter.drawEllipse(0,0,165,165);
}
To copy to clipboard, switch view to plain text mode
Bookmarks