[Qt-interest] QString doesn't print anything with qDebug

Sean Harmer sean.harmer at maps-technology.com
Wed Dec 1 14:18:20 CET 2010


On Wednesday 01 December 2010 12:22:02 Anisha Kaul wrote:
> Hello again,
> 
> My XML file is filled with relevant data like this:
> 
> <?xml version="1.0"?>
> 
> <CONFIGINFO>
> 
>         <ABC>
>                 <portNM>3794</portNM>
>                 <timoutPeriod>10</timoutPeriod>
>         </ABC>
> 
> ...
> 
> I wrote a small code to print the tag names, and no characters EXCEPT THE
> SPACES get shown up when I run it. The relevant (successfully compiled)
> code is presented here:
> ___________________________________________________________________________
> __________________ XmlFileReader :: XmlFileReader (string xmlFileName)
> {
>     QFile                   *objQFile    = new QFile ("config.xml");
>     if (objQFile->open (QIODevice :: ReadOnly) == false)
>     {
>             cout << "\nError: Unable to open the XML file! Verify the
> file's existence and permissions\n\n"; exit (0);
>     }
> 
>     QXmlStreamReader        objQXmlReader (objQFile);
> 
>     QXmlStreamReader :: TokenType token = objQXmlReader.readNext();
>     QXmlStreamReader :: TokenType token = objQXmlReader.readNext();
> 
>     QString s = objQXmlReader.readElementText ();
> 
>     qDebug("--- %s ---",s.toAscii().data());
>     qDebug("%s", qPrintable(s));
> 
>     QXmlStreamReader :: TokenType token2 = objQXmlReader.readNext();
> 
>     QString s2 = objQXmlReader.readElementText ();
>     qDebug("--- %s ---",s2.toAscii().data());
>     qDebug("%s", qPrintable(s2));
> }
> ___________________________________________________________________________
> __________________
> 
> Please see if someone can guide me on this.

Please read the docs. QXmlStreamReader::readElementText() reads the text 
within an element and not the tag name. In your particular case I think you 
are trying to print out the text contents of the <ABC> tag - which of course 
is just a bunch of spaces. Use QXmlStreamReader::name() to get the name of the 
tag when you encounter a start element.

HTH,

Sean



More information about the Qt-interest-old mailing list