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. Qt scrolling a image within QLabel
Forum Update on Monday, May 27th 2025

Qt scrolling a image within QLabel

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 750 Views
  • 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 26 Feb 2015, 09:30 last edited by
    #1

    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 Reply Last reply
    0

    1/1

    26 Feb 2015, 09:30

    • Login

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