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 "videoplayer.h"
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QtWidgets/QDesktopWidget>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include <QtCore/QDir>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, 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; 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(); player.show();
return app.exec(); return app.exec();

View File

@@ -54,20 +54,20 @@ VideoPlayer::VideoPlayer(QWidget *parent)
QVideoWidget *videoWidget = new QVideoWidget; QVideoWidget *videoWidget = new QVideoWidget;
QAbstractButton *openButton = new QPushButton(tr("Open...")); QAbstractButton *openButton = new QPushButton(tr("Open..."));
connect(openButton, SIGNAL(clicked()), this, SLOT(openFile())); connect(openButton, &QAbstractButton::clicked, this, &VideoPlayer::openFile);
playButton = new QPushButton; playButton = new QPushButton;
playButton->setEnabled(false); playButton->setEnabled(false);
playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(playButton, SIGNAL(clicked()), connect(playButton, &QAbstractButton::clicked,
this, SLOT(play())); this, &VideoPlayer::play);
positionSlider = new QSlider(Qt::Horizontal); positionSlider = new QSlider(Qt::Horizontal);
positionSlider->setRange(0, 0); positionSlider->setRange(0, 0);
connect(positionSlider, SIGNAL(sliderMoved(int)), connect(positionSlider, &QAbstractSlider::sliderMoved,
this, SLOT(setPosition(int))); this, &VideoPlayer::setPosition);
errorLabel = new QLabel; errorLabel = new QLabel;
errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum); errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
@@ -86,11 +86,13 @@ VideoPlayer::VideoPlayer(QWidget *parent)
setLayout(layout); setLayout(layout);
mediaPlayer.setVideoOutput(videoWidget); mediaPlayer.setVideoOutput(videoWidget);
connect(&mediaPlayer, SIGNAL(stateChanged(QMediaPlayer::State)), connect(&mediaPlayer, &QMediaPlayer::stateChanged,
this, SLOT(mediaStateChanged(QMediaPlayer::State))); this, &VideoPlayer::mediaStateChanged);
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64))); connect(&mediaPlayer, &QMediaPlayer::positionChanged, this, &VideoPlayer::positionChanged);
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64))); connect(&mediaPlayer, &QMediaPlayer::durationChanged, this, &VideoPlayer::durationChanged);
connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError())); typedef void (QMediaPlayer::*ErrorSignal)(QMediaPlayer::Error);
connect(&mediaPlayer, static_cast<ErrorSignal>(&QMediaPlayer::error),
this, &VideoPlayer::handleError);
} }
VideoPlayer::~VideoPlayer() VideoPlayer::~VideoPlayer()
@@ -99,14 +101,23 @@ VideoPlayer::~VideoPlayer()
void VideoPlayer::openFile() void VideoPlayer::openFile()
{ {
errorLabel->setText(""); 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());
}
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath()); void VideoPlayer::setUrl(const QUrl &url)
{
if (!fileName.isEmpty()) { errorLabel->setText(QString());
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName)); setWindowFilePath(url.isLocalFile() ? url.toLocalFile() : QString());
playButton->setEnabled(true); mediaPlayer.setMedia(url);
} playButton->setEnabled(true);
} }
void VideoPlayer::play() void VideoPlayer::play()
@@ -151,5 +162,11 @@ void VideoPlayer::setPosition(int position)
void VideoPlayer::handleError() void VideoPlayer::handleError()
{ {
playButton->setEnabled(false); 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 QAbstractButton;
class QSlider; class QSlider;
class QLabel; class QLabel;
class QUrl;
QT_END_NAMESPACE QT_END_NAMESPACE
class VideoPlayer : public QWidget class VideoPlayer : public QWidget
@@ -59,6 +60,8 @@ public:
VideoPlayer(QWidget *parent = 0); VideoPlayer(QWidget *parent = 0);
~VideoPlayer(); ~VideoPlayer();
void setUrl(const QUrl &url);
public slots: public slots:
void openFile(); void openFile();
void play(); void play();