Search code examples
c++qtqgraphicsitemqmediaplayer

How to mask video in Qgraphicsvideoitem of any shape


I am using Qgraphicsvideoitem to show video using Qmediaplayer. Now If I have item of round shape how do I mask the video into that.

Here is my code and image is:

enter image description here

VideoClass::VideoClass(QGraphicsVideoItem *parent):QGraphicsVideoItem(parent)
{
    QMediaPlayer *player = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
    QMediaPlaylist *playlist = new QMediaPlaylist;
    playlist->addMedia(QUrl::fromLocalFile("/root/Desktop/Content/Robotica_1080.wmv"));
    playlist->setPlaybackMode(QMediaPlaylist::Loop);
    player->setPlaylist(playlist);
    player->play();
    player->setVideoOutput(this);
}

void VideoClass::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QGraphicsVideoItem::paint(painter, option, widget);
    painter->drawEllipse(0, 0, 400, 400);
}

QRectF VideoClass::boundingRect() const
{
    return QRectF(0,0,400,400);}

Solution

  • I think you want to crop the video into path that you are painting. If it is so,use shape and opaqueArea vitual functions to do this. And dont forget to set flag QGraphicsItem::ItemClipsToShape.