5

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);
  • Did not see anything in the doc http://doc.qt.io/qt-5/videooverview.html - apparently a direction could be to display the video with [`QGraphicsVideoItem`](http://doc.qt.io/qt-5/qgraphicsvideoitem.html#details) and the subtitles with [`QGraphicsTextItem`](http://doc.qt.io/qt-5/qgraphicstextitem.html#details) in a `QGraphicsScene`. – ymoreau Jan 17 '18 at 08:42

1 Answers1

1

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.

G.M.
  • 12,232
  • 2
  • 15
  • 18
  • Thank you :) it really helped.the other problem is that application should allow to save annotations in a specific form ( in this program). Don't really get how it should work ;/ – Jacek Duraj Jan 17 '18 at 13:44
  • That's really a separate question but, have a look at [SubRip/SRT Subtitles](https://matroska.org/technical/specs/subtitles/srt.html). – G.M. Jan 17 '18 at 13:53
  • Yes i thought about srt but i dont really know how to handle them in my program – Jacek Duraj Jan 17 '18 at 15:18
  • okay i did this your way. But now i have a problem because i created my own VideoWidget and i have no idea of how to add it to my mainwindow. I can't use setCentralWidget because i already have one central widget that includes buttons. Any ideas?? – Jacek Duraj Jan 18 '18 at 00:30
  • @JacekDuraj If this answer solved the problem presented in the question you should accept it. Don't bloat the answer with comments or follow-up questions not directly related to an answer but ask a separate question. Accepting an answer is important as it both rewards posters for solving your problem and informs others that your issue is resolved. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green. – talamaki Jan 18 '18 at 06:27