I was trying to loop through all children of specific XML node and join their name
attributes. The structure:
<params>
<param name="BLAH" />
</params>
The desired result:
PARAM1='$PARAM1',PARAM2='$PARAM2',PARAM3='$PARAM3'[...]
The code:
// Create empty text stream
QTextStream paramNames("");
// Start looping child by child
QDomElement child = params.firstChildElement();
bool firstIteration = true;
while( !child.isNull() ) {
QString param_name = child.attribute("n");
// Skips empty names
if(param_name.length()>0) {
// This prevents both leading and trailing comma
if(!firstIteration)
paramNames<<",";
else
firstIteration = false;
// This should fill in one entry
paramNames<<param_name<<"='$"<<param_name<<'\'';
}
child = child.nextSiblingElement();
}
Now even the debugger says that if I do
QString paramNamesSTR = paramNames.readAll();
the paramNamesSTR
is an empty string. However if I use std
library instead, everything works:
std::stringstream paramNames("");
QDomElement child = params.firstChildElement();
bool firstIteration = true;
while( !child.isNull() ) {
std::string param_name = child.attribute("n").toUtf8().constData();
if(param_name.length()>0) {
if(!firstIteration)
paramNames<<",";
else
firstIteration = false;
paramNames<<param_name<<"='$"<<param_name<<'\'';
}
child = child.nextSiblingElement();
}
QString paramNamesSTR = QString::fromStdString( paramNames.str() );
So what's the difference? Why does the Qt QTextStream
return empty string? I would really prefer to be consistent with used libraries and therefore use the QTextStream
rather than std::stringstream
, although presonally, I prefer the former.