How do I use foreach with QDomNodeList in Qt?

Posted by Venemo on Stack Overflow See other posts from Stack Overflow or by Venemo
Published on 2010-05-26T15:57:24Z Indexed on 2010/05/26 16:01 UTC
Read the original article Hit count: 357

Filed under:
|
|
|
|

Hi Everyone,

I'm new to Qt and I'm learning something new every day.
Currently, I'm developing a small application for my Nokia N900 in my free time.
Everything is fine, I am able to compile and run Maemo applications on the device.

I've just learned about the foreach keyword in Qt. (I know it is not in C++, so I didn't think about it until I accidentally stumbled upon a Qt doc that mentioned it.)
So, I decided to change my quite annoying and unreadable loops to foreach, but I failed with this:

QDomNodeList list = doc.lastChild().childNodes().at(1).firstChild().childNodes();
for (int x = 0; x < list.count(); x++)
{
    QDomElement node = list.at(x).toElement();
    // Do something with node
}

This is how I tried:

foreach (QDomElement node, doc.lastChild().childNodes().at(1).firstChild().childNodes())
{
    // Do something with node
}

For some reason the above code doesn't even compile. I get cryptic error messages from the compiler.

Could someone please explain to me how to get it right?

If the foreach loop doesn't support QDomNodeList, is there a way of handling XML files that do support foreach?

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt