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)
|
, mediaPlayer(0, QMediaPlayer::VideoSurface)
|
||||||
, playButton(0)
|
, playButton(0)
|
||||||
, positionSlider(0)
|
, positionSlider(0)
|
||||||
|
, errorLabel(0)
|
||||||
{
|
{
|
||||||
QVideoWidget *videoWidget = new QVideoWidget;
|
QVideoWidget *videoWidget = new QVideoWidget;
|
||||||
|
|
||||||
@@ -68,6 +69,9 @@ VideoPlayer::VideoPlayer(QWidget *parent)
|
|||||||
connect(positionSlider, SIGNAL(sliderMoved(int)),
|
connect(positionSlider, SIGNAL(sliderMoved(int)),
|
||||||
this, SLOT(setPosition(int)));
|
this, SLOT(setPosition(int)));
|
||||||
|
|
||||||
|
errorLabel = new QLabel;
|
||||||
|
errorLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
|
||||||
|
|
||||||
QBoxLayout *controlLayout = new QHBoxLayout;
|
QBoxLayout *controlLayout = new QHBoxLayout;
|
||||||
controlLayout->setMargin(0);
|
controlLayout->setMargin(0);
|
||||||
controlLayout->addWidget(openButton);
|
controlLayout->addWidget(openButton);
|
||||||
@@ -77,6 +81,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
|
|||||||
QBoxLayout *layout = new QVBoxLayout;
|
QBoxLayout *layout = new QVBoxLayout;
|
||||||
layout->addWidget(videoWidget);
|
layout->addWidget(videoWidget);
|
||||||
layout->addLayout(controlLayout);
|
layout->addLayout(controlLayout);
|
||||||
|
layout->addWidget(errorLabel);
|
||||||
|
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
|
|
||||||
@@ -85,6 +90,7 @@ VideoPlayer::VideoPlayer(QWidget *parent)
|
|||||||
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
|
this, SLOT(mediaStateChanged(QMediaPlayer::State)));
|
||||||
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
|
connect(&mediaPlayer, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
|
||||||
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
|
connect(&mediaPlayer, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));
|
||||||
|
connect(&mediaPlayer, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError()));
|
||||||
}
|
}
|
||||||
|
|
||||||
VideoPlayer::~VideoPlayer()
|
VideoPlayer::~VideoPlayer()
|
||||||
@@ -93,11 +99,12 @@ VideoPlayer::~VideoPlayer()
|
|||||||
|
|
||||||
void VideoPlayer::openFile()
|
void VideoPlayer::openFile()
|
||||||
{
|
{
|
||||||
|
errorLabel->setText("");
|
||||||
|
|
||||||
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Movie"),QDir::homePath());
|
||||||
|
|
||||||
if (!fileName.isEmpty()) {
|
if (!fileName.isEmpty()) {
|
||||||
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
|
mediaPlayer.setMedia(QUrl::fromLocalFile(fileName));
|
||||||
|
|
||||||
playButton->setEnabled(true);
|
playButton->setEnabled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,3 +147,9 @@ void VideoPlayer::setPosition(int position)
|
|||||||
{
|
{
|
||||||
mediaPlayer.setPosition(position);
|
mediaPlayer.setPosition(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VideoPlayer::handleError()
|
||||||
|
{
|
||||||
|
playButton->setEnabled(false);
|
||||||
|
errorLabel->setText("Error: " + mediaPlayer.errorString());
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@
|
|||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QAbstractButton;
|
class QAbstractButton;
|
||||||
class QSlider;
|
class QSlider;
|
||||||
|
class QLabel;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
class VideoPlayer : public QWidget
|
class VideoPlayer : public QWidget
|
||||||
@@ -67,11 +68,13 @@ private slots:
|
|||||||
void positionChanged(qint64 position);
|
void positionChanged(qint64 position);
|
||||||
void durationChanged(qint64 duration);
|
void durationChanged(qint64 duration);
|
||||||
void setPosition(int position);
|
void setPosition(int position);
|
||||||
|
void handleError();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMediaPlayer mediaPlayer;
|
QMediaPlayer mediaPlayer;
|
||||||
QAbstractButton *playButton;
|
QAbstractButton *playButton;
|
||||||
QSlider *positionSlider;
|
QSlider *positionSlider;
|
||||||
|
QLabel *errorLabel;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user