Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create 4 rows of moving picture in Main Window
Qt 6.11 is out! See what's new in the release blog

Create 4 rows of moving picture in Main Window

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 656 Views 1 Watching
  • 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 Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    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
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved