1

我想在 currentFrameNumber 等于 frameCount 时使用 frameChanged(int) 发出信号,我的 QT 版本是 4.7.4

QObject::connect(movie,SIGNAL(frameChanged(int)),movie,SLOT(stop()));

QMovieChild.cpp:

class QMovieChild:public QMovie
{

signals:
void newSignal();
public slots:
    void onFrameChanged(int frameNumber){
        if(frameNumber == this->frameCount()-2)
            emit newSignal();
}
};

怪物.cpp:

void Monster::Vanishment()
{
QMovieChild *movie = new QMovieChild();
movie->setFileName("./pikaqiu.gif");
QSize size = this->getLabelPointer()->size();
movie->setScaledSize(size);
this->getLabelPointer()->setMovie(movie);
QMovie::connect(movie,SIGNAL(newSignal()),movie,SLOT(deleteLater()));
QMovie::connect(movie,SIGNAL(frameChanged(int)),movie,SLOT(onFrameChanged(int)));
movie->start();
}
4

1 回答 1

1

要创建一个类应该从 QObject 继承的信号,如果你使用的类是 QWidget,它也是有效的,因为它是从 QObject 继承的。

#ifndef QMOVIECHILD_H
#define QMOVIECHILD_H

#include <QMovie>

class QMovieChild : public QMovie
{
    Q_OBJECT
public:
    QMovieChild(QObject * parent = 0):QMovie(parent){
        connect(this, SIGNAL(frameChanged(int)), this, SLOT(onFrameChanged(int)));
    }

signals:
    void newSignal();
private slots:
    void onFrameChanged(int frameNumber){
        if(frameNumber == frameCount()-1){
            emit newSignal(); 
            //stop();
        }
    }

};

#endif // QMOVIECHILD_H

利用:

QMovieChild *movie = new QMovieChild;
movie->setFileName("./pikaqiu.gif");
QMovie::connect(movie,SIGNAL(newSignal()),movie,SLOT(deleteLater()));
QSize size = getLabelPointer()->size();
movie->setScaledSize(size);
getLabelPointer()->setMovie(movie);
movie->start();
于 2017-07-06T02:36:17.077 回答