Example: Notify user of errors

Before, the widget simply failed silently, which gave the impression
that the widget is broken.

Change-Id: I8ab7ed0e0a62f9643791b6f4732f7f3b2cd7521a
Reviewed-by: Martin Smith <martin.smith@digia.com>
This commit is contained in:
Sze Howe Koh
2012-12-19 00:11:29 +08:00
committed by The Qt Project
parent d13a7a1f89
commit 404b7dbe3f
2 changed files with 17 additions and 1 deletions

View File

@@ -49,6 +49,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
, mediaPlayer(0, QMediaPlayer::VideoSurface)
, playButton(0)
, positionSlider(0)
, errorLabel(0)
{
QVideoWidget *videoWidget = new QVideoWidget;
@@ -68,6 +69,9 @@ VideoPlayer::VideoPlayer(QWidget *parent)
connect(positionSlider, SIGNAL(sliderMoved(int)),
this, SLOT(setPosition(int)));
errorLabel = new QLabel;
errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
QBoxLayout *controlLayout = new QHBoxLayout;
controlLayout->setMargin(0);
controlLayout->addWidget(openButton);
@@ -77,6 +81,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
QBoxLayout *layout = new QVBoxLayout;
layout->addWidget(videoWidget);
layout->addLayout(controlLayout);
layout->addWidget(errorLabel);
setLayout(layout);
@@ -85,6 +90,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
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()));
}
VideoPlayer::~VideoPlayer()
@@ -93,11 +99,12 @@ 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);
}
}
@@ -140,3 +147,9 @@ void VideoPlayer::setPosition(int position)
{
mediaPlayer.setPosition(position);
}
void VideoPlayer::handleError()
{
playButton->setEnabled(false);
errorLabel->setText("Error: " + mediaPlayer.errorString());
}