• 14836阅读
  • 9回复

如何使用QMovie类实现.gif和.mng动画图片的播放(来自官方EX)(源代码) [复制链接]

上一主题 下一主题
离线jzj139
 
只看楼主 倒序阅读 楼主  发表于: 2007-06-12
— 本帖被 XChinux 执行加亮操作(2008-07-18) —
#include <qapplication.h>
#include <qfiledialog.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qmessagebox.h>
#include <qmovie.h>
#include <qvbox.h>


class MovieScreen : public //QFrameThe QFrame class is the base class of widgets that can have a frame. It draws a frame and calls a virtual function, drawContents(), to fill in the frame. This function is reimplemented by subclasses.
{
    Q_OBJECT//对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必需的。
    QMovie movie;//The QMovie class provides incremental loading of animations or images, signalling as it progresses. The simplest way to display a QMovie is to use a QLabel and QLabel::setMovie(). A QMovie provides a QPixmap as the framePixmap(); connections can be made via connectResize() and connectUpdate() to receive notification of size and pixmap changes. All decoding is driven by the normal event-processing mechanisms.
    QString filename;//The QString class provides an abstraction of Unicode text and the classic C '\0'-terminated char array.
    QSize sh;//The QSize class defines the size of a two-dimensional object. qsize类定义一个二维对象的大小。

public:
    MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
        QFrame(p, name, f),
    sh(100,100)
    {
        setCaption(fname);
        filename = fname;
        movie = m;

        // Set a frame around the movie.
        setFrameStyle(QFrame::WinPanel|QFrame::Sunken);

        // No background needed, since we draw on the whole widget.
        movie.setBackgroundColor(backgroundColor());
        setBackgroundMode(NoBackground);

        // Get the movie to tell use when interesting things happen.
        movie.connectUpdate(this, SLOT(movieUpdated(const QRect&)));
        movie.connectResize(this, SLOT(movieResized(const QSize&)));
        movie.connectStatus(this, SLOT(movieStatus(int)));

    setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));//大小布局可以拉伸
    }

    QSize sizeHint() const
    {
    return sh;
    }

protected:

    // Draw the contents of the QFrame - the movie and on-screen-display
    void drawContents(QPainter* p)
    {
        // Get the current movie frame.
        QPixmap pm = movie.framePixmap();

        // Get the area we have to draw in.
        QRect r = contentsRect();

    if ( !pm.isNull() ) {
        // Only rescale is we need to - it can take CPU!
        if ( r.size() != pm.size() ) {
        QWMatrix m;
        m.scale((double)r.width()/pm.width(),
            (double)r.height()/pm.height());
        pm = pm.xForm(m);
        }

        // Draw the [possibly scaled] frame.  movieUpdated() below calls
        // repaint with only the changed area, so clipping will ensure we
        // only do the minimum amount of rendering.
        //
        p->drawPixmap(r.x(), r.y(), pm);
    }


        // The on-screen display

        const char* message = 0;

        if (movie.paused()) {
            message = "PAUSED";
        } else if (movie.finished()) {
            message = "THE END";
        } else if (movie.steps() > 0) {
            message = "FF >>";
        }

        if (message) {
            // Find a good font size...
            p->setFont(QFont("Helvetica", 24));

            QFontMetrics fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(QFont("Helvetica", 18));

            fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(QFont("Helvetica", 14));

            fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(QFont("Helvetica", 12));

            fm = p->fontMetrics();
            if ( fm.width(message) > r.width()-10 )
                p->setFont(QFont("Helvetica", 10));

            // "Shadow" effect.
            p->setPen(black);
            p->drawText(1, 1, width()-1, height()-1, AlignCenter, message);
            p->setPen(white);
            p->drawText(0, 0, width()-1, height()-1, AlignCenter, message);
        }
    }

public slots:
    void restart()
    {
    movie.restart();
        repaint();
    }

    void togglePause()
    {
    if ( movie.paused() )
        movie.unpause();
    else
        movie.pause();
        repaint();
    }

    void step()
    {
    movie.step();
        repaint();
    }

    void step10()
    {
    movie.step(10);
        repaint();
    }

private slots:
    void movieUpdated(const QRect& area)
    {

        if (!isVisible())
            show();

        // The given area of the movie has changed.

        QRect r = contentsRect();

        if ( r.size() != movie.framePixmap().size() ) {
            // Need to scale - redraw whole frame.
            repaint( r );
        } else {
            // Only redraw the changed area of the frame
            repaint( area.x()+r.x(), area.y()+r.x(),
                     area.width(), area.height() );
        }
    }

    void movieResized(const QSize& size)
    {
        // The movie changed size, probably from its initial zero size.

        int fw = frameWidth();
        sh = QSize( size.width() + fw*2, size.height() + fw*2 );
    updateGeometry();
    if ( parentWidget() && parentWidget()->isHidden() )
        parentWidget()->show();
    }

    void movieStatus(int status)
    {
        // The movie has sent us a status message.

        if (status < 0) {
        QString msg;
        msg.sprintf("Could not play movie \"%s\"", (const char*)filename);
        QMessageBox::warning(this, "movies", msg);
        parentWidget()->close();
        } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) {
            repaint(); // Ensure status text is displayed
        }
    }
};

class MoviePlayer : public QVBox {
    MovieScreen* movie;
public:
    MoviePlayer(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) :
    QVBox(p,name,f)
    {
    movie = new MovieScreen(fname, m, this);
    QHBox* hb = new QHBox(this);
    QPushButton* btn;
    btn = new QPushButton("<<", hb);
    connect(btn, SIGNAL(clicked()), movie, SLOT(restart()));
    btn = new QPushButton("||", hb);
    connect(btn, SIGNAL(clicked()), movie, SLOT(togglePause()));
    btn = new QPushButton(">|", hb);
    connect(btn, SIGNAL(clicked()), movie, SLOT(step()));
    btn = new QPushButton(">>|", hb);
    connect(btn, SIGNAL(clicked()), movie, SLOT(step10()));
    }
};


// A QFileDialog that chooses movies.
//
class MovieStarter: public QFileDialog {
    Q_OBJECT
public:
    MovieStarter(const char *dir);

public slots:
    void startMovie(const QString& filename);
    // QDialog's method - normally closes the file dialog.
    // We want it left open, and we want Cancel to quit everything.
    void done( int r );
};


MovieStarter::MovieStarter(const char *dir)
    : QFileDialog(dir, "*.gif *.mng")
{
    //behave as in getOpenFilename
    setMode( ExistingFile );
    // When a file is selected, show it as a movie.
    connect(this, SIGNAL(fileSelected(const QString&)),
        this, SLOT(startMovie(const QString&)));
}


void MovieStarter::startMovie(const QString& filename)
{
    if ( filename ) // Start a new movie - have it delete when closed.
    (new MoviePlayer( filename, QMovie(filename), 0, 0,
                   WDestructiveClose))->show();
}

void MovieStarter::done( int r )
{
    if (r != Accepted)
    qApp->quit(); // end on Cancel
    setResult( r );

    // And don't hide.
}


int main(int argc, char **argv)
{
    QApplication a(argc, argv);

    if (argc > 1) {
        // Commandline mode - show movies given on the command line
        //
    bool gui=TRUE;
        for (int arg=1; arg<argc; arg++) {
        if ( QString(argv[arg]) == "-i" )
        gui = !gui;
        else if ( gui )
        (void)new MoviePlayer(argv[arg], QMovie(argv[arg]), 0, 0,
                      Qt::WDestructiveClose);
        else
        (void)new MovieScreen(argv[arg], QMovie(argv[arg]), 0, 0,
                      Qt::WDestructiveClose);
    }
        QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));
    } else {
        // "GUI" mode - open a chooser for movies
        //
        MovieStarter* fd = new MovieStarter(".");
        fd->show();
    }

    // Go!
    return a.exec();
}

#include "main.moc"
[ 此贴被XChinux在2008-07-18 14:43重新编辑 ]
qt
离线wvins
只看该作者 1楼 发表于: 2008-12-10
正在QMovie的问题上卡壳...

不是果然这么复杂吧,还以为下面这样的代码就成了呢
--------------------------
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QMovie *mvSkater= new QMovie("Skater.gif"); //使用相对路径导致出错。
                                                                              //frameCount()=-1 
    QLabel *lblGIF = new QLabel("aaaa");
    //lblGIF->setMovie(mvSkater);
    lblGIF->show();
    return app.exec();
}
---------------------------
直接在这个基础上改吧。
另外,QMovie也不能直接从资源文件创建,比如:
new QMovie(":/Gif/Skater.gif");                            //ERROR
正确的做法是
        file = new QFile(":/Gif/Skater.gif");
        file->open(QIODevice::ReadOnly);
        mvSkate = new QMovie(file);
当然,这就要求,这里的file必须保证生命期不短于QMovie
[ 此贴被wvins在2008-12-10 20:17重新编辑 ]
离线wvins
只看该作者 2楼 发表于: 2008-12-10
貌似楼主的代码不是Qt4的...
离线laurentium
只看该作者 3楼 发表于: 2009-04-17
QMovie放gif,你在一个widget上同时放几个就知道了,卡得要死
我同时放10个,cpu 50%
放100个,gif已然不是gif了,变成静态图片了
离线wwwxxlby
只看该作者 4楼 发表于: 2009-04-21
官方源码需要修理整改。用setmovie函数的结果是无法放大缩小显示GIF。因此修改源码可以实现你想要的结果。大家可以一起来探讨!
离线wwwxxlby
只看该作者 5楼 发表于: 2009-04-21
本人修改了一下


MovieScreen::MovieScreen(const char* fname, QMovie m, QRect rec, QWidget* px, const char* name, WFlags f)
          :QLabel(px, name, f)        
    {
        setCaption(fname);
        filename = fname;
        movie = m;
        int x_set;
        int y_set;
        x_set=(480-rec.width())/2;
        y_set=(178-rec.height())/2;//主要是我的显示屏是480*272的,所以显示区域我自定义的。
        setGeometry(QRect(x_set,y_set, rec.width(), rec.height()));
        setFrameStyle(QFrame::Plain);
        movie.setBackgroundColor(backgroundColor());
        setBackgroundMode(NoBackground);
       movie.connectUpdate(this, SLOT(movieUpdated(const QRect&)));
       movie.connectResize(this, SLOT(movieResized(const QSize&)));
        setSizePolicy(QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding));
    }
      void  MovieScreen::drawContents(QPainter* p)
    {
            QPixmap pm = movie.framePixmap();        
            QRect r =frameGeometry();        
        if ( !pm.isNull() ) {
                 if ( r.size() != pm.size() ) {
                QWMatrix m;
                m.scale((double)r.width()/pm.width(),
                        (double)r.height()/pm.height());
                pm = pm.xForm(m);
            }      
            //p->drawPixmap(r.x(), r.y(), pm);
            p->drawPixmap(0, 0, pm);
           }
    }  

    void  MovieScreen::movieUpdated(const QRect& area)
    {
        if (!isVisible())
            show();
        QRect r = contentsRect();

        if ( r.size() != movie.framePixmap().size() ) {
               repaint( r );
        } else {
                    repaint( area.x()+r.x(), area.y()+r.x(),
                     area.width(), area.height() );
        }
    }

    void  MovieScreen::movieResized(const QSize& size)
    {
        int fw = frameWidth();
        sh = QSize( size.width() + fw*2, size.height() + fw*2 );
        updateGeometry();
        if ( parentWidget() && parentWidget()->isHidden() )
            parentWidget()->show();
    }
  可以显示按自己设定的大小的GIF,欢迎大家来探讨。
离线laurentium
只看该作者 6楼 发表于: 2009-06-03
有没有人研究过用QMovie放gif的效率?
离线onglus
只看该作者 7楼 发表于: 2009-09-18
5楼朋友,你的代码好像没有加载动画图像文件,怎么能播放嘛!
Qt高阶编程交流群71555992,进群条件是非常勿扰。
离线onglus
只看该作者 8楼 发表于: 2009-09-18
哎呀,感觉大家每个人的方法都讲得好乱,没有一点条理性,有的程序一看就知道存在问题。对于动态显示.gif文件,我已经实现了,我用的是自己的方法。我想和大家讨论一下,如何加载一个指明的文件,而不是每次到文件目录下面去找。我用一楼的方法试过,根本不得行。大家愿意讨论的,可以加入我的Qt4编程群71555992,大家一起来讨论下!
Qt高阶编程交流群71555992,进群条件是非常勿扰。
离线toby520

只看该作者 9楼 发表于: 2011-10-11
共同探讨
QtQML多多指教开发社区 http://qtclub.heilqt.com
将QtCoding进行到底
关注移动互联网,关注金融
开发跨平台客户端,服务于金融行业
专业定制界面
群号:312125701   373955953(qml控件定做)
快速回复
限100 字节
 
上一个 下一个