Polish the videowidget example.

- Add command line parsing and file argument.
- Port to Qt 5 connection syntax.
- Adapt size to available geometry (for High DPI).

Task-number: QTBUG-53114
Change-Id: I1c1c547ddb14210ef5900f99f4870d6d91b67088
Reviewed-by: Yoann Lopes <yoann.lopes@qt.io>
This commit is contained in:
Friedemann Kleint
2016-05-03 11:21:17 +02:00
parent 1afe2ed975
commit 6f3e6a78ca
3 changed files with 62 additions and 19 deletions

View File

@@ -41,13 +41,36 @@
#include "videoplayer.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QDesktopWidget>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include <QtCore/QDir>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QCoreApplication::setApplicationName("Video Widget Example");
QCoreApplication::setOrganizationName("QtProject");
QGuiApplication::setApplicationDisplayName(QCoreApplication::applicationName());
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QCommandLineParser parser;
parser.setApplicationDescription("Qt Video Widget Example");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("url", "The URL to open.");
parser.process(app);
VideoPlayer player;
player.resize(320, 240);
if (!parser.positionalArguments().isEmpty()) {
const QUrl url =
QUrl::fromUserInput(parser.positionalArguments().constFirst(),
QDir::currentPath(), QUrl::AssumeLocalFile);
player.setUrl(url);
}
const QRect availableGeometry = QApplication::desktop()->availableGeometry(&player);
player.resize(availableGeometry.width() / 6, availableGeometry.height() / 4);
player.show();
return app.exec();

View File

@@ -54,20 +54,20 @@ VideoPlayer::VideoPlayer(QWidget *parent)
QVideoWidget *videoWidget = new QVideoWidget;
QAbstractButton *openButton = new QPushButton(tr("Open..."));
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile()));
connect(openButton, &QAbstractButton::clicked, this, &VideoPlayer::openFile);
playButton = new QPushButton;
playButton->setEnabled(false);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()),
this, SLOT(play()));
connect(playButton, &QAbstractButton::clicked,
this, &VideoPlayer::play);
positionSlider = new QSlider(Qt::Horizontal);
positionSlider->setRange(0, 0);
connect(positionSlider, SIGNAL(sliderMoved(int)),
this, SLOT(setPosition(int)));
connect(positionSlider, &QAbstractSlider::sliderMoved,
this, &VideoPlayer::setPosition);
errorLabel = new QLabel;
errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
@@ -86,11 +86,13 @@ VideoPlayer::VideoPlayer(QWidget *parent)
setLayout(layout);
mediaPlayer.setVideoOutput(videoWidget);
connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)),
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
connect(&mediaPlayer, &QMediaPlayer::stateChanged,
this, &VideoPlayer::mediaStateChanged);
connect(&mediaPlayer, &QMediaPlayer::positionChanged, this, &VideoPlayer::positionChanged);
connect(&mediaPlayer, &QMediaPlayer::durationChanged, this, &VideoPlayer::durationChanged);
typedef void (QMediaPlayer::*ErrorSignal)(QMediaPlayer::Error);
connect(&mediaPlayer, static_cast<ErrorSignal>(&QMediaPlayer::error),
this, &VideoPlayer::handleError);
}
VideoPlayer::~VideoPlayer()
@@ -99,14 +101,23 @@ VideoPlayer::~VideoPlayer()
void VideoPlayer::openFile()
{
errorLabel->setText("");
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
if (!fileName.isEmpty()) {
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
playButton->setEnabled(true);
QFileDialog fileDialog(this);
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setWindowTitle(tr("Open Movie"));
QStringList supportedMimeTypes = mediaPlayer.supportedMimeTypes();
if (!supportedMimeTypes.isEmpty())
fileDialog.setMimeTypeFilters(supportedMimeTypes);
fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation).value(0, QDir::homePath()));
if (fileDialog.exec() == QDialog::Accepted)
setUrl(fileDialog.selectedUrls().constFirst());
}
void VideoPlayer::setUrl(const QUrl &url)
{
errorLabel->setText(QString());
setWindowFilePath(url.isLocalFile() ? url.toLocalFile() : QString());
mediaPlayer.setMedia(url);
playButton->setEnabled(true);
}
void VideoPlayer::play()
@@ -151,5 +162,11 @@ void VideoPlayer::setPosition(int position)
void VideoPlayer::handleError()
{
playButton->setEnabled(false);
errorLabel->setText("Error: " + mediaPlayer.errorString());
const QString errorString = mediaPlayer.errorString();
QString message = "Error: ";
if (errorString.isEmpty())
message += " #" + QString::number(int(mediaPlayer.error()));
else
message += errorString;
errorLabel->setText(message);
}

View File

@@ -50,6 +50,7 @@ QT_BEGIN_NAMESPACE
class QAbstractButton;
class QSlider;
class QLabel;
class QUrl;
QT_END_NAMESPACE
class VideoPlayer : public QWidget
@@ -59,6 +60,8 @@ public:
VideoPlayer(QWidget *parent = 0);
~VideoPlayer();
void setUrl(const QUrl &url);
public slots:
void openFile();
void play();