Example: Notify user of errors
authorSze Howe Koh <szehowe.koh@gmail.com>
Tue, 18 Dec 2012 16:11:29 +0000 (00:11 +0800)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 19 Dec 2012 10:07:34 +0000 (11:07 +0100)
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>
examples/multimediawidgets/videowidget/videoplayer.cpp
examples/multimediawidgets/videowidget/videoplayer.h

index d961a63..4083543 100644 (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());
+}
index ddad187..0bd720b 100644 (file)
@@ -49,6 +49,7 @@
 QT_BEGIN_NAMESPACE
 class QAbstractButton;
 class QSlider;
+class QLabel;
 QT_END_NAMESPACE
 
 class VideoPlayer : public QWidget
@@ -67,11 +68,13 @@ private slots:
     void positionChanged(qint64 position);
     void durationChanged(qint64 duration);
     void setPosition(int position);
+    void handleError();
 
 private:
     QMediaPlayer mediaPlayer;
     QAbstractButton *playButton;
     QSlider *positionSlider;
+    QLabel *errorLabel;
 };
 
 #endif