Qt scrolling a image within QLabel
-
wrote on 26 Feb 2015, 09:30 last edited by
Manage to create Bullet and every 50msec, bullet pixmap is moved.
But i want to create within QLabel. Please assist.@ --Bullet.c--
Bullet::Bullet(QGraphicsItem *parent): QObject(), QGraphicsPixmapItem(parent){
// draw graphics
setPixmap(QPixmap(":/images/bullet.png"));// make a timer to move bullet every 50ms QTimer * timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(move())); // start the timer every 50 ms timer->start(50);
}
void Bullet::move(){
// move the bullet forward
setPos(x()-10,y());
// if the bullet is off the screen, destroy it
if (pos().x() < 0){
scene()->removeItem(this);
delete this;
}
}//---Bullet.h
#ifndef BULLET_H
#define BULLET_H#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QObject>class Bullet: public QObject,public QGraphicsPixmapItem {
Q_OBJECT
public:
Bullet(QGraphicsItem * parent=0);
public slots:
void move();
};#endif // BULLET_H
@
@ main.cpp
// create the scene scene = new QGraphicsScene(); scene->setSceneRect(0,0,800,600); ui->graphicsView->setScene(scene); // Label created imageLabel = new QLabel; imageLabel ->setBackgroundRole(QPalette::Base); //Bullet created Bullet * bullet = new Bullet(); bullet->setPos(x(),y()); scene()->addItem(bullet);
@
1/1