📄 movies-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - movies/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Movies<br>Or the Story of the Animated GIF file</h1><br clear="all"> The Movies example displays animated GIF files using the QMovie and QLabel classes. <hr> Main:<pre>/****************************************************************************** $Id: qt/examples/movies/main.cpp 2.3.8 edited 2004-05-12 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <<a name="qapplication.h"></a><a href="qapplication-h.html">qapplication.h</a>>#include <<a name="qfiledialog.h"></a><a href="qfiledialog-h.html">qfiledialog.h</a>>#include <<a name="qlabel.h"></a><a href="qlabel-h.html">qlabel.h</a>>#include <<a name="qpainter.h"></a><a href="qpainter-h.html">qpainter.h</a>>#include <<a name="qmessagebox.h"></a><a href="qmessagebox-h.html">qmessagebox.h</a>>#include <<a name="qmovie.h"></a><a href="qmovie-h.html">qmovie.h</a>>class MovieScreen : public QFrame { Q_OBJECT <a name="QMovie"></a><a href="qmovie.html">QMovie</a> movie; <a name="QString"></a><a href="qstring.html">QString</a> filename;public: MovieScreen(const char* fname, QMovie m, QWidget* p=0, const char* name=0, WFlags f=0) : <a name="QFrame"></a><a href="qframe.html">QFrame</a>(p, name, f) { 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))); }protected: // Draw the contents of the QFrame - the movie and on-screen-display void drawContents(<a name="QPainter"></a><a href="qpainter.html">QPainter</a>* p) { // Get the current movie frame. <a name="QPixmap"></a><a href="qpixmap.html">QPixmap</a> pm = movie.framePixmap(); // Get the area we have to draw in. <a name="QRect"></a><a href="qrect.html">QRect</a> r = contentsRect(); // Only rescale is we need to - it can take CPU! if ( r.<a name="size"></a><a href="qrect.html#507cd9">size</a>() != pm.<a name="size"></a><a href="qpixmap.html#60b65f">size</a>() ) { <a name="QWMatrix"></a><a href="qwmatrix.html">QWMatrix</a> m; m.<a name="scale"></a><a href="qwmatrix.html#41a356">scale</a>((double)r.<a name="width"></a><a href="qrect.html#45fe95">width</a>()/pm.<a name="width"></a><a href="qpixmap.html#86dbc5">width</a>(), (double)r.<a name="height"></a><a href="qrect.html#581ab8">height</a>()/pm.<a name="height"></a><a href="qpixmap.html#d5bb33">height</a>()); pm = pm.<a name="xForm"></a><a href="qpixmap.html#ff5fcf">xForm</a>(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-><a name="drawPixmap"></a><a href="qpainter.html#c64b89">drawPixmap</a>(r.<a name="x"></a><a href="qrect.html#fccae7">x</a>(), r.<a name="y"></a><a href="qrect.html#f448f7">y</a>(), 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-><a name="setFont"></a><a href="qpainter.html#998df2">setFont</a>(<a name="QFont"></a><a href="qfont.html">QFont</a>("Helvetica", 24)); <a name="QFontMetrics"></a><a href="qfontmetrics.html">QFontMetrics</a> fm = p-><a name="fontMetrics"></a><a href="qpainter.html#73b2e5">fontMetrics</a>(); if ( fm.<a name="width"></a><a href="qfontmetrics.html#3b6f39">width</a>(message) > r.<a href="qrect.html#45fe95">width</a>()-10 ) p-><a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 18)); fm = p-><a href="qpainter.html#73b2e5">fontMetrics</a>(); if ( fm.<a href="qfontmetrics.html#3b6f39">width</a>(message) > r.<a href="qrect.html#45fe95">width</a>()-10 ) p-><a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 14)); fm = p-><a href="qpainter.html#73b2e5">fontMetrics</a>(); if ( fm.<a href="qfontmetrics.html#3b6f39">width</a>(message) > r.<a href="qrect.html#45fe95">width</a>()-10 ) p-><a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 12)); fm = p-><a href="qpainter.html#73b2e5">fontMetrics</a>(); if ( fm.<a href="qfontmetrics.html#3b6f39">width</a>(message) > r.<a href="qrect.html#45fe95">width</a>()-10 ) p-><a href="qpainter.html#998df2">setFont</a>(<a href="qfont.html">QFont</a>("Helvetica", 10)); // "Shadow" effect. p->setPen(black); p-><a name="drawText"></a><a href="qpainter.html#0f088f">drawText</a>(1, 1, width()-1, height()-1, AlignCenter, message); p-><a name="setPen"></a><a href="qpainter.html#0183e4">setPen</a>(white); p-><a href="qpainter.html#0f088f">drawText</a>(0, 0, width()-1, height()-1, AlignCenter, message); } } void mouseReleaseEvent(<a name="QMouseEvent"></a><a href="qmouseevent.html">QMouseEvent</a>* event) { // Do what the Help says... if (event-><a name="state"></a><a href="qmouseevent.html#db99dc">state</a>() & ShiftButton) { movie.restart(); } else if (!movie.paused()) { movie.pause(); } else { if (event-><a name="button"></a><a href="qmouseevent.html#78482f">button</a>() & LeftButton) movie.step((event-><a href="qmouseevent.html#db99dc">state</a>() & ControlButton) ? 10 : 1); else if (event-><a href="qmouseevent.html#78482f">button</a>() & (MidButton|RightButton)) movie.unpause(); } repaint(); // To hide/show "PAUSED". }private slots: void movieUpdated(const QRect& area) { if (!isVisible()) show(); // The given area of the movie has changed. <a href="qrect.html">QRect</a> r = contentsRect(); if ( r.<a href="qrect.html#507cd9">size</a>() != movie.framePixmap().size() ) { // Need to scale - redraw whole frame. repaint( r ); } else { // Only redraw the changed area of the frame repaint( area.x()+r.<a href="qrect.html#fccae7">x</a>(), area.y()+r.<a href="qrect.html#fccae7">x</a>(), area.width(), area.height() ); } } void movieResized(const QSize& size) { // The movie changed size, probably from its initial zero size. int fw = frameWidth(); resize( size.width() + fw*2, size.height() + fw*2 ); qApp->setMainWidget(this); // Just geometry, etc. qApp->setMainWidget(0); // Not Close==Quit } void movieStatus(int status) { // The movie has sent us a status message. if (status < 0) { <a href="qstring.html">QString</a> msg; msg.<a name="sprintf"></a><a href="qstring.html#926f67">sprintf</a>("Could not play movie \"%s\"", (const char*)filename); <a name="QMessageBox::message"></a><a href="qmessagebox.html#6f1d2d">QMessageBox::message</a>("Error", msg); } else if (status == QMovie::Paused || status == QMovie::EndOfMovie) { repaint(); // Ensure status text is displayed } }};// A QFileDialog that chooses movies.//class MovieStarter : public QFileDialog { Q_OBJECTpublic: MovieStarter(const char *dir) : <a name="QFileDialog"></a><a href="qfiledialog.html">QFileDialog</a>(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&))); }public slots: void startMovie(const QString& filename) { if ( filename ) // Start a new movie - have it delete when closed. (void)new MovieScreen(filename, QMovie(filename), 0, 0, WDestructiveClose); } // QDialog's method - normally closes the file dialog. // We want it left open, and we want Cancel to quit everything. void done( int r ) { if (r != Accepted) qApp->quit(); // end on Cancel setResult( r ); // And don't hide. }};int main(int argc, char **argv){ <a name="QApplication"></a><a href="qapplication.html">QApplication</a> a(argc, argv); if (argc > 1) { // Commandline mode - show movies given on the command line // for (int arg=1; arg<argc; arg++) (void)new MovieScreen(argv[arg], QMovie(argv[arg]), 0, 0, Qt::WDestructiveClose); <a name="QObject::connect"></a><a href="qobject.html#7f8e37">QObject::connect</a>(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit())); } else { // "GUI" mode - open a chooser for movies // MovieStarter* fd = new MovieStarter("."); fd-><a name="show"></a><a href="qdialog.html#bfe29c">show</a>(); // Some help text to explain the `hidden' features. <a name="QLabel"></a><a href="qlabel.html">QLabel</a>* help = new <a href="qlabel.html">QLabel</a>( "Choose some movies.\n\n" "Shift-click to Restart.\n" "Click to Pause,\n" "then left-click to Step,\n" "control-left-click to Step 10,\n" "right-click to Unpause\n\n" "Windows may be resized to enlarge movie.", 0 ); help-><a name="setCaption"></a><a href="qwidget.html#d6a291">setCaption</a>( "Qt Examples - Movies" ); help-><a name="setIndent"></a><a href="qlabel.html#9077ed">setIndent</a>( 10 ); help-><a name="show"></a><a href="qwidget.html#200ee5">show</a>(); } // Go! return a.<a name="exec"></a><a href="qapplication.html#84c7bf">exec</a>();}#include "main.moc"</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -