如何将QVideoWidget设置为全屏并在全屏小部件中具有音量滑块?

时间:2018-09-03 09:33:44

标签: c++ qt

我正在开发一个简单的视频循环应用程序,想知道是否可以将音量滑块控件添加到 QVideoWidget 的全屏模式。

我希望该应用在启动时显示一个小的 QVideoWidget 。然后,双击视频窗口小部件后,我希望视频可以通过附加的音量滑块全屏显示。

目前,我有一个基于 QMultimedia 示例的代码,如下所示:

videowidget.h

#ifndef VIDEOWIDGET_H
#define VIDEOWIDGET_H

#include <QVideoWidget>

class VideoWidget : public QVideoWidget
{
    Q_OBJECT
public:
    VideoWidget(QWidget *parent = 0);

protected:
    void keyPressEvent(QKeyEvent *event);
    void mouseDoubleClickEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
};

#endif // VIDEOWIDGET_H

videowidget.cpp

void VideoWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
    setFullScreen(!isFullScreen());
    event->accept();
}

我能想到的一个解决方案是创建一个基本的小部件来绘制另一个videowidget和一个滑块并将其加载到双击事件中,但是我很难实现它。我不知道如何将媒体播放器的当前播放状态转移到新的视频小部件。

1 个答案:

答案 0 :(得分:0)

#include <QApplication>
#include <QMouseEvent>
#include <QVideoWidget>
#include <QSlider>
#include <QMediaPlayer>
#include <QFileDialog>

/**
 * @brief The VideoWidget class
 *
 * VideoWidget contains QSlider & QVideoWidget
 */
class VideoWidget : public QWidget
{
    Q_OBJECT
    QVideoWidget* videoWidget;
    QSlider* slider;
public:
    VideoWidget() :
        videoWidget(new QVideoWidget(this)),
        slider(new QSlider(Qt::Horizontal, this))
    {
        slider->hide();
        slider->move(10, 10); // Put slider where you want.
        slider->setRange(0, 100);

        const QMediaContent meida(QFileDialog::getOpenFileUrl());
        QMediaPlayer* player = new QMediaPlayer(this);
        player->setVideoOutput(videoWidget);
        player->setMedia(meida);
        player->play();

        slider->setValue(player->volume());
        connect(slider, &QSlider::valueChanged, player, &QMediaPlayer::setVolume);
    }
    virtual ~VideoWidget() {}
protected:
    /**
     * @brief changeEvent
     * @param event
     *
     * Listen to window state change event,
     * and show the slider only when the state is WindowFullScreen.
     */
    void changeEvent(QEvent* event) {
        if(event->type() == QEvent::WindowStateChange)
            slider->setVisible(windowState() == Qt::WindowFullScreen);
        QWidget::changeEvent(event);
    }

    /**
     * @brief resizeEvent
     * @param event
     * 
     * Make videoWidget fill the parent widget.
     */
    void resizeEvent(QResizeEvent* event) {
        videoWidget->resize(size());
        event->accept();
    }

    void mouseDoubleClickEvent(QMouseEvent *event) {
        if(!isFullScreen())
            showFullScreen();
        else
            showNormal();
        event->accept();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    VideoWidget w;
    w.resize(500, 400);
    w.show();

    return a.exec();
}

#include "main.moc"