Create 4 rows of moving picture in Main Window



  • Need to create 4 rows of moving pictures in Main Window.
    Thinking it is wiser to embed the moving pictures in each QLabel.
    Below is the code that create Bullet and every 50msec bullet pixmap is moved across
    But i want to create it within QLabel. Please comment

    @
    --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);
     @

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.