From 5abd67e0645d2382861f86f6d3c6772e7727a811 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 8 Oct 2014 11:53:21 +0200 Subject: Polish schema example. Add some error handling, fix warning on startup: QIODevice::read: device not open caused by MainWindow::schemaSelected(0) which clears the instance combo causing the current index to temporarily change to -1. Change-Id: I213d68b07b2dee4e2bbc1404aaf149ce7c189713 Reviewed-by: Joerg Bornemann --- examples/xmlpatterns/schema/mainwindow.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'examples/xmlpatterns/schema/mainwindow.cpp') diff --git a/examples/xmlpatterns/schema/mainwindow.cpp b/examples/xmlpatterns/schema/mainwindow.cpp index 0de5309..bb18e38 100644 --- a/examples/xmlpatterns/schema/mainwindow.cpp +++ b/examples/xmlpatterns/schema/mainwindow.cpp @@ -130,8 +130,15 @@ void MainWindow::schemaSelected(int index) } textChanged(); - QFile schemaFile(QString(":/schema_%1.xsd").arg(index)); - schemaFile.open(QIODevice::ReadOnly); + const QString fileName = QStringLiteral(":/schema_") + + QString::number(index) + QStringLiteral(".xsd"); + QFile schemaFile(fileName); + if (!schemaFile.open(QIODevice::ReadOnly)) { + qWarning() << "Cannot open" << QDir::toNativeSeparators(fileName) + << ':' << schemaFile.errorString(); + return; + } + const QString schemaText(QString::fromUtf8(schemaFile.readAll())); schemaView->setPlainText(schemaText); @@ -142,8 +149,19 @@ void MainWindow::schemaSelected(int index) //! [2] void MainWindow::instanceSelected(int index) { - QFile instanceFile(QString(":/instance_%1.xml").arg((2*schemaSelection->currentIndex()) + index)); - instanceFile.open(QIODevice::ReadOnly); + if (index < 0) { + instanceEdit->setPlainText(QString()); + return; + } + const QString fileName = QStringLiteral(":/instance_") + + QString::number(2 * schemaSelection->currentIndex() + index) + + QStringLiteral(".xml"); + QFile instanceFile(fileName); + if (!instanceFile.open(QIODevice::ReadOnly)) { + qWarning() << "Cannot open" << QDir::toNativeSeparators(fileName) + << ':' << instanceFile.errorString(); + return; + } const QString instanceText(QString::fromUtf8(instanceFile.readAll())); instanceEdit->setPlainText(instanceText); -- cgit v1.2.1