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:
committed by
The Qt Project
parent
d13a7a1f89
commit
404b7dbe3f
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user