diff options
author | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2014-10-08 11:53:21 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@digia.com> | 2014-10-08 12:32:41 +0200 |
commit | 5abd67e0645d2382861f86f6d3c6772e7727a811 (patch) | |
tree | 14bb74b95b2639dd9885846b8109297cf8195bed | |
parent | eef0ba60200d8039c05073ce707c2352ac0944a8 (diff) | |
download | qtxmlpatterns-5abd67e0645d2382861f86f6d3c6772e7727a811.tar.gz qtxmlpatterns-5abd67e0645d2382861f86f6d3c6772e7727a811.tar.bz2 qtxmlpatterns-5abd67e0645d2382861f86f6d3c6772e7727a811.zip |
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 <joerg.bornemann@digia.com>
-rw-r--r-- | examples/xmlpatterns/schema/mainwindow.cpp | 26 |
1 files changed, 22 insertions, 4 deletions
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); |