[SOLVED] QTextStream problem !

  • hi,

    I am using this piece of code to validate an XML file against an XML scheam. Once the XML file is valide, I use QTextStream to read the file content as QString then display it on the terminal :

    // open file
    QFile file(filename);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    qDebug() << "Cannot open file ";

    // validate file against XML schema
    QXmlSchemaValidator validator(schema);

    if(!validator.validate(&file, QUrl::fromLocalFile(file.fileName())))
    qDebug() << "instance document is invalid";

    QTextStream textstream(&file);
    QString xmlContent = textstream.readAll();

    But, the qDebug() function display an empty string ("") !

    Any idea ?

    thanks in advance.

  • Lifetime Qt Champion


    A shot in the dark but, since the validator must read the file in order to do the validation, the current file position might be at the end.

    Try to add


    before creating the QTextStream.

    Also since you are reading all the file anyway, why not read it all in a QByteArray, then validate that array ?

  • Thank you, it works now ;
    thanks. :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.