Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Create 4 rows of moving picture in Main Window

    General and Desktop
    1
    1
    367
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      houmingc last edited by

      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);
       @
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post