qtqvideowidget

Adding label to video


I have to write a simple video player that can display some subtitles,link or a picture(like on YouTube) in a certain time. I have no idea of how do display anything using QVideoWidget. I couldn't find any useful class to do it. Could you please give me some advices?

I did It your way but after i load any video QLabel disappears...

player->setVideoOutput(vw);
playlistView->setMaximumWidth(200);
playlistView->setMinimumWidth(300);

window = new QWidget;

Playerlayout = new QGridLayout;

subtitleWidget = new QLabel;

subtitleWidget->setMaximumWidth(1000);
subtitleWidget->setMaximumHeight(100);
subtitleWidget->setStyleSheet("QLabel {background-color : red; color 
blue;}");

subtitleWidget->setAlignment(Qt::AlignCenter | Qt::AlignBottom);
subtitleWidget->setWordWrap(true);
subtitleWidget->setText("example subtitle");



Playerlayout->addWidget(vw,0,0);

Playerlayout->addWidget(subtitleWidget,0,0);

Playerlayout->addWidget(playlistView,0,1,1,2);

Solution

  • If QVideoWidget doesn't provide what you require directly then you could always set up an overlay.

    The basic layout item hierarchy would be something like...

    QWidget
      layout
        QVideoWidget
        subtitle_widget
    

    In this case the layout could be either a QStackedLayout using stacking mode QStackedLayout::StackAll or a QGridLayout with both the QVideoWidget and the subtitle_widget occupying the same cells but with the correct z-order.

    Going with the QGridLayout...

    auto *w = new QWidget;
    auto *l = new QGridLayout(w);
    auto *video_widget = new QVideoWidget;
    auto *subtitle_widget = new QLabel;
    
    /*
     * Subtitles will be shown at the bottom of the 'screen'
     * and centred horizontally.
     */
    subtitle_widget->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    subtitle_widget->setWordWrap(true);
    
    /*
     * Place both the video and subtitle widgets in cell (0, 0).
     */
    l->addWidget(video_widget, 0, 0);
    l->addWidget(subtitle_widget, 0, 0);
    

    Subtitles etc. can now be displayed simply by invoking subtitle_widget->setText(...) at the appropriate time.

    The same method can easily be extended to overlaying other types of information.