iaterck
Registrado: 17 May 2010 Mensajes: 10
| Publicado: 04/11/2011 9:59 am | | | Título: prblemas con animacion Qt. |
| necesito ayuda en qt de c++... quiero q cambia de forma mi figura por cada rebote q de... espero me puedan ayudar.
| Código: | animacion.h
#ifndef ANIMACION_H #define ANIMACION_H
#include <QWidget>
class animacion : public QWidget { Q_OBJECT public: explicit animacion(QWidget *parent = 0); void mover(int x,int y); protected: void paintEvent(QPaintEvent *event); //void paintEvent(QPaintEvent *event); private: int x,y; int dirx,diry;
signals:
public slots:
};
#endif // ANIMACION_H
|
| Código: | mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include "animacion.h" #include <QTimer>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow { Q_OBJECT
public: explicit MainWindow(QWidget *parent = 0); ~MainWindow();
private slots: void on_Pintar_clicked(); void mover();
private: Ui::MainWindow *ui; animacion *panimacion; QTimer *cronometro; };
#endif // MAINWINDOW_H
|
| Código: | animacion.cpp #include "animacion.h" #include <QPainter>
animacion::animacion(QWidget *parent) : QWidget(parent) { x = y = 0; dirx = diry = 0; }
void animacion :: paintEvent(QPaintEvent *) {
QPainter painter(this); painter.setBrush(Qt::white); painter.drawRect(0,0,width() ,height());
painter.setBrush(Qt::red); painter.drawEllipse(x,y,20,20);
}
void animacion :: mover(int x, int y){
if (dirx == 0){
this-> x += x;
} else this-> x -=x;
if (diry == 0) this-> y += y;
else this-> y -= y;
/*cambia*/
if(this->x+20 >= width()) dirx = 1; if(this->x <= 0) dirx = 0;
if(this->y+20 >= height()) diry = 1; if(this->y < 0) diry = 0;
update(); }
|
| Código: | main.cpp
#include <QtGui/QApplication> #include "mainwindow.h"
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show();
return a.exec(); }
|
| Código: | mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); panimacion = new animacion(this); ui->areaDibujo->addWidget(panimacion); cronometro = new QTimer(this); connect(cronometro,SIGNAL(timeout()),this,SLOT(mover())); }
MainWindow::~MainWindow() { delete ui; }
void MainWindow::on_Pintar_clicked() {
if(cronometro->isActive()) cronometro->stop();
else cronometro->start(0.0000000000000000000000000001); }
void MainWindow::mover(){
panimacion->mover(10,10);
} [quote][/quote] |
|
|