为视频添加标签

Adding label to video

我必须编写一个简单的视频播放器,可以在特定时间内显示一些字幕、link 或图片(如 YouTube 上的)。我不知道如何使用 QVideoWidget 显示任何内容。我找不到任何有用的 class 来做这件事。你能给我一些建议吗?

我按照你的方式做了,但是在我加载任何视频后 QLabel 消失了...

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);

如果 QVideoWidget 没有直接提供您需要的内容,那么您可以随时设置覆盖。

基本布局项目层次结构类似于...

QWidget
  layout
    QVideoWidget
    subtitle_widget

在这种情况下,布局可以是 QStackedLayout using stacking mode QStackedLayout::StackAll or a QGridLayout,其中 QVideoWidgetsubtitle_widget 占据相同的单元格,但具有正确的 z 顺序。

选择 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);

现在只需在适当的时候调用 subtitle_widget->setText(...) 即可显示字幕等。

同样的方法可以很容易地扩展到覆盖其他类型的信息。