I'm using Qt in c++ to code plants z zombies which involves me moving things across the screen. To do that, I'm using QTimer which I've connected to the advance() function.
I'm able to get my object to move (using qDebug and seeing the coordinates) but I don't physically see the picture change unless I refresh the screen or switch windows (e.g. pressing alt tab twice or clicking the windows key button)
This is an example of one of my classes. Here I'm trying to get my peas move across the screen.
#ifndef BULLETS_H
#define BULLETS_H
#include <QPoint>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QRectF>
#include <QPainter>
#include <QVector>
#include <QImage>
#include <QWidget>
//#include "mainwindow.h"
class bullets : public QGraphicsItem
{
public:
QRectF boundingRect() const {return QRectF(this->x,0, 20,20);}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
bullets(int xPlant, int yPlant, QImage bullet);
~bullets() {}
void advance(int phase); //Moves the bullet across the screen
int x;
int y;
QGraphicsPixmapItem *bulletsPixMap;
QImage pea_picture;
int velocity;
};
#endif
along with the corresponding cpp file
#include "bullets.h"
#include <QDebug>
#include <QPainter>
int counter = 0;
bullets::bullets(int xPlant, int yPlant,QImage bullet)
{
velocity = 10;
x = xPlant;
y = yPlant;
pea_picture = bullet;
}
void bullets::advance(int phase)
{
if (!phase)
return;
x += (velocity * 0.5);
// setPos(x,y);
}
void bullets::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawPixmap(this->x,this->y,QPixmap::fromImage(pea_picture));
/*
QBrush b(QColor(0,252,0));
painter->setBrush(b);
painter->drawEllipse(boundingRect());
*/
}