diff options
-rw-r--r-- | examples/multimediawidgets/videowidget/main.cpp | 25 | ||||
-rw-r--r-- | examples/multimediawidgets/videowidget/videoplayer.cpp | 53 | ||||
-rw-r--r-- | examples/multimediawidgets/videowidget/videoplayer.h | 3 |
3 files changed, 62 insertions, 19 deletions
diff --git a/examples/multimediawidgets/videowidget/main.cpp b/examples/multimediawidgets/videowidget/main.cpp index c9940e10e..fd726884b 100644 --- a/examples/multimediawidgets/videowidget/main.cpp +++ b/examples/multimediawidgets/videowidget/main.cpp @@ -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(); diff --git a/examples/multimediawidgets/videowidget/videoplayer.cpp b/examples/multimediawidgets/videowidget/videoplayer.cpp index c3554ff04..8504746c4 100644 --- a/examples/multimediawidgets/videowidget/videoplayer.cpp +++ b/examples/multimediawidgets/videowidget/videoplayer.cpp @@ -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()); + 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()); +} - if (!fileName.isEmpty()) { - mediaPlayer.setMedia(QUrl::fromLocalFile(fileName)); - playButton->setEnabled(true); - } +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); } diff --git a/examples/multimediawidgets/videowidget/videoplayer.h b/examples/multimediawidgets/videowidget/videoplayer.h index 24589f542..f9f3b692b 100644 --- a/examples/multimediawidgets/videowidget/videoplayer.h +++ b/examples/multimediawidgets/videowidget/videoplayer.h @@ -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(); |