动态调整QGraphicScene中的视频大小

时间:2015-10-08 18:50:32

标签: c++ qt qgraphicsview qgraphicsscene qmediaplayer

所以我有一个QMediaPlayer播放视频,它包含在QGraphicsScene内部的QGraphicsVideoItem中。我也在QGraphicsScene里面有文字。 QGraphicsView调整大小为文本大小,但不调整视频大小,因此我只能看到视频的左上角。

有没有办法让视频拉伸以填充可用空间?有没有办法将视频置于可用空间中,而不是卡在左上角?

以下是我目前的代码。

videoWidget = new VideoWidget();
QGraphicsView* graphicsView = new QGraphicsView(videoWidget);
QGraphicsScene* scene = new QGraphicsScene(graphicsView);
QGraphicsVideoItem* videoItem = new QGraphicsVideoItem();
QGraphicsTextItem* text = new QGraphicsTextItem("Lorem ipsum ");

text->setDefaultTextColor(Qt::white);
graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

player = new QMediaPlayer();
player->setVideoOutput(videoItem);

graphicsView->setScene(scene);
graphicsView->scene()->addItem(videoItem);
graphicsView->scene()->addItem(text);

playlist = new QMediaPlaylist();
playlist->setPlaybackMode(QMediaPlaylist::Loop);
player->setPlaylist(playlist);

playlistModel = new PlaylistModel();
playlistModel->setPlaylist(playlist);`

1 个答案:

答案 0 :(得分:0)

所以我想出了一种调整视频大小的方法,基本上我在videoWidget中实例化了resizeEvent,以便更新图形项的大小。

void VideoWidget::resizeEvent(QResizeEvent *event)
{
  QSize size = event->size();

  if(graphicsView != NULL)
    {//Changes the size of the area the video can display in
      graphicsView->setMinimumSize(size);
      graphicsView->setMaximumSize(size);
    }
  if(graphicsVideoItem != NULL)
    {//This changes the size/pos of the video itself
      graphicsVideoItem->setSize(size);
    }
  QVideoWidget::resizeEvent(event);
}

class VideoWidget : public QVideoWidget