Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Video Widget Example

The Video Widget example shows how to implement a video widget using QtMultimedia's QAbstractVideoSurface

VideoWidgetSurface Class Definition


The VideoWidgetSurface class inherits QAbstractVideoSurface and paints video frames on a QWidget. This is a separate class to VideoWidget as both QAbstractVideoSurface and QWidget inherit QObject.

In addition to the functions from QAbstractVideoSurface, VideoWidgetSurface has functions for determining the video display rectangle, and painting the video.

VideoWidgetSurface Class Implementation


From the supportedPixelFormats() function we return a list of pixel formats the surface can paint. The order of the list hints at which formats are preferred by the surface. Assuming a 32-bit RGB backbuffer, we'd expect that a 32-bit RGB type with no alpha to be fastest to paint so QVideoFrame::Image_RGB32 is first in the list.

Since we don't support rendering using any special frame handles we don't return any pixel formats if handleType is not QAbstractVideoBuffer::NoHandle.


In isFormatSupported() we test if the frame type of a surface format maps to a valid QImage format, that the frame size is not empty, and the handle type is QAbstractVideoBuffer::NoHandle. Note that the QAbstractVideoSurface implementation of isFormatSupported() will verify that the list of supported pixel formats returned by supportedPixelFormats(format.handleType()) contains the pixel format and that the size is not empty so a reimplementation wasn't strictly necessary in this case.


To start our surface we'll extract the image format and size from the selected video format and save it for use in the paint() function. If the image format, or size are invalid then we'll set an error and return false. Otherwise we'll save the format and confirm the surface has been started, by calling QAbstractVideoSurface::start(). Finally since the video size may have changed we'll trigger an update of the widget, and video geometry.


The updateVideoRect() function calculates the region within the widget the video occupies. The size hint of the video format gives a suggested size for the video calculated from the viewport and pixel aspect ratio. If the suggested size fits within the widget then we create a new rect of that size in the center of the widget. Otherwise we shrink the size maintaining the aspect ratio so that it does fit.


We can't paint from outside a paint event, so when a new frame is received in present() we save a reference to it and force an immediate repaint of the video region. We retain the saved reference to the frame after the repaint so that the widget can be repainted between frame changes if necessary.

If the format of the frame doesn't match the surface format we can't paint it or very likely any future frames. So we set an UnsupportedFormatError on our surface and stop it immediately.


The paint() function is called by the video widget to paint the current video frame. Before we draw the frame first we'll check the format for the scan line direction and if the scan lines are arranged from bottom to top we'll flip the painter so the frame isn't drawn upside down. Then using the image format information saved in the start() function we'll construct a new QImage from the current video frame, and draw it to the the widget.


When the surface is stopped we need to release the current frame and invalidate the video region. Then we confirm the surface has been stopped by calling QAbstractVideoSurface::stop() which sets the started state to false and finally we update so the video widget so paints over the last frame.

VideoWidget Class Definition

The VideoWidget class uses the VideoWidgetSurface class to implement a video widget.


The VideoWidget QWidget implementation is minimal with just the sizeHint(), paintEvent(), and resizeEvent() functions in addition to the constructor, destructor and an instance of VideoWidgetSurface.

VideoWidget Class Implementation


In the VideoWidget constructor we set some flags to speed up re-paints a little. Setting the Qt::WA_NoSystemBackground flag and disabling automatic background fills will stop Qt from a painting a background that'll be completely obscured by the video. The Qt::WA_PaintOnScreen flag will allow us to paint to the screen instead of the back buffer where supported.

Next we set the background color to black, so that any borders around the video are filled in black rather the default background color.

Finally we construct an instance of the VideoWidgetSurface class.


In the destructor we simply delete the VideoWidgetSurface instance.


We get the size hint for the widget from the video format of the surface which is calculated from viewport and pixel aspect ratio of the video format.


When the video widget receives a paint event we first check if the surface is started, if not then we simply fill the widget with the background color. If it is then we draw a border around the video region clipped by the paint region, before calling paint on the video surface to draw the current frame.


The resizeEvent() function is reimplemented to trigger an update of the video region when the widget is resized.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web