//! [3] void MainWindow::validate() { const QByteArray schemaData = schemaView->toPlainText().toUtf8(); const QByteArray instanceData = instanceEdit->toPlainText().toUtf8(); MessageHandler messageHandler; QXmlSchema schema; schema.setMessageHandler(&messageHandler); schema.load(schemaData); bool errorOccurred = false; if (!schema.isValid()) { errorOccurred = true; } else { QXmlSchemaValidator validator(schema); if (!validator.validate(instanceData)) errorOccurred = true; } if (errorOccurred) { validationStatus->setText(messageHandler.statusMessage()); moveCursor(messageHandler.line(), messageHandler.column()); } else { validationStatus->setText(tr("validation successful")); } const QString styleSheet = QString("QLabel {background: %1; padding: 3px}") .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() : QColor(Qt::green).lighter(160).name()); validationStatus->setStyleSheet(styleSheet); }
bool MainWindow::validate() { QUrl url("qrc:/resources/peach.xsd"); const QByteArray instanceData = completingTextEdit->toPlainText().toUtf8(); MessageHandler messageHandler; QXmlSchema schema; schema.setMessageHandler(&messageHandler); schema.load(url); bool errorOccurred = false; if (!schema.isValid()) { errorOccurred = true; } else { QXmlSchemaValidator validator(schema); if (!validator.validate(instanceData)) errorOccurred = true; } if (errorOccurred) { statusLabel->setText(messageHandler.statusMessage()); moveCursor(messageHandler.line(), messageHandler.column()); return false; } else { statusLabel->setText(tr("Validation successful")); return true; } const QString styleSheet = QString("QstatusLabel {background: %1; padding: 3px}") .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() : QColor(Qt::green).lighter(160).name()); statusLabel->setStyleSheet(styleSheet); }