From 1ddd4f8f0641948aabfd1031a080720e8dc61984 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 5 Mar 2014 13:21:31 +0100 Subject: [PATCH] Add some error handling to the QQuickWidget example. Use a QMainWindow and display errors occurring during loading QML as well as scene graph errors (for example, failure to create an Open GL context) in the status bar. Change-Id: Icd9ad2bd2b809baf80d83a54e295c25b7fb1f94b Reviewed-by: Laszlo Agocs --- examples/quickwidgets/quickwidget/main.cpp | 65 ++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/examples/quickwidgets/quickwidget/main.cpp b/examples/quickwidgets/quickwidget/main.cpp index 7251760..c130434 100644 --- a/examples/quickwidgets/quickwidget/main.cpp +++ b/examples/quickwidgets/quickwidget/main.cpp @@ -39,33 +39,74 @@ ****************************************************************************/ #include +#include #include -int main(int argc, char **argv) -{ - QApplication app(argc, argv); +class MainWindow : public QMainWindow { + Q_OBJECT +public: + MainWindow(); - QMdiArea *toplevel = new QMdiArea; +private slots: + void quickWidgetStatusChanged(QQuickWidget::Status); + void sceneGraphError(QQuickWindow::SceneGraphError error, const QString &message); + +private: + QQuickWidget *m_quickWidget; +}; + +MainWindow::MainWindow() + : m_quickWidget(new QQuickWidget) +{ + QMdiArea *centralWidget = new QMdiArea; QLCDNumber *lcd = new QLCDNumber; lcd->display(1337); lcd->setMinimumSize(250,100); - toplevel->addSubWindow(lcd); + centralWidget ->addSubWindow(lcd); QUrl source("qrc:quickwidget/rotatingsquare.qml"); - QQuickWidget *w = new QQuickWidget; - w->resize(300,300); - w->setResizeMode(QQuickWidget::SizeRootObjectToView ); - w->setSource(source); + connect(m_quickWidget, SIGNAL(statusChanged(QQuickWidget::Status)), + this, SLOT(quickWidgetStatusChanged(QQuickWidget::Status))); + connect(m_quickWidget, SIGNAL(sceneGraphError(QQuickWindow::SceneGraphError,QString)), + this, SLOT(sceneGraphError(QQuickWindow::SceneGraphError,QString))); + m_quickWidget->resize(300,300); + m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView ); + m_quickWidget->setSource(source); - toplevel->addSubWindow(w); + centralWidget ->addSubWindow(m_quickWidget); - toplevel->show(); + setCentralWidget(centralWidget); - return app.exec(); + QMenu *fileMenu = menuBar()->addMenu(tr("File")); + QAction *quitAction = fileMenu->addAction(tr("Quit")); + connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); +} + +void MainWindow::quickWidgetStatusChanged(QQuickWidget::Status status) +{ + if (status == QQuickWidget::Error) { + QStringList errors; + foreach (const QQmlError &error, m_quickWidget->errors()) + errors.append(error.toString()); + statusBar()->showMessage(errors.join(QStringLiteral(", "))); + } } +void MainWindow::sceneGraphError(QQuickWindow::SceneGraphError, const QString &message) +{ + statusBar()->showMessage(message); +} +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + + MainWindow mainWindow; + mainWindow.show(); + return app.exec(); +} +#include "main.moc" -- 2.7.4