c++xmlqtqxmlstreamreader

QXmlStreamReader: can't follow the flow of readNextStartElement()


I have to implemenent a xml parser that constructs and places Qt objects in a widget in runtime. I wrote a parser based on this. A module represents the QWidget that will hold the QPushButton and QCheckBox instances. xml is a QXmlStreamReader, just like the example from Qt reference.

So, here i try to parse through this very basic xml file:

<somexml version="1.0">
    <module>
        <button label="Send UART" define="BUTTON0" pos="30, 30" size="20, 10">
            <action cmd="sendCom" data="0xAA 0xBB 0xCC"/>
        </button>
        <checkbox label="Send UART" define="CHECKBOX0" pos="250, 30" size="20, 10">
            <action cmd="sendCom" data="test"/>
        </checkbox>
        <button label="Check BIT" define="BUTTON1" pos="140, 30" size="20, 10">
            <action cmd="setDef" data="BUTTON0=1"/>
        </button>   
    </module>
</somexml>

A module Element will create a widget to hold the checkboxes and pushbuttons within the tags. So here:

void XmlReader::readXml()
{
    while (xml.readNextStartElement())
    {
        if (xml.name() == "module")
        {
            readModule();
        }
    }
}

I expect to read the module element and enter the scope of readModule, which it does. Inside of readModule i really have trouble following the flow of parsing.

Here is the implementation:

void XmlReader::readModule()
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "module");


    while (xml.readNextStartElement())
    {
        if (xml.name() == "button")
        {
            readButton(widget);
        }
        else if (xml.name() == "checkbox")
        {
            readCheckBox(widget);
        }
        else
        {
            xml.skipCurrentElement();
        }
    }
}

It will only reach the first button element in the xml file and create one button. Send UART.

When i change the implementation of readXml to:

void XmlReader::readXml()
{
    while (xml.readNextStartElement())
    {
        if (xml.name() == "module")
        {
            readModule();
        }
        else if (xml.name() == "button")
        {
            readButton(widget);
        }
        else if (xml.name() == "checkbox")
        {
            readCheckBox(widget);
        }
        else
        { 
            xml.skipCurrentElement();
        }
    }
}

it will reach the first button and the first checkbox. What am i missing?

EDIT: added implementation of readButton

void XmlReader::readButton(Widget *widget)
{
    Q_ASSERT(xml.isStartElement() && xml.name() == "button");

    QString label = xml.attributes().value("label").toString();
    QString define = xml.attributes().value("define").toString();

    QString pos = xml.attributes().value("pos").toString();
    QStringList posList = pos.split(",");
    int posX = posList[0].toInt();
    int posY = posList[1].toInt();

    QString size = xml.attributes().value("size").toString();
    QStringList sizeList= size.split(",");
    int sizeX = sizeList[0].toInt();
    int sizeY = sizeList[1].toInt();

    QString cmd, data;

    while (xml.readNextStartElement())
    {
        if (xml.name() == "action") 
        {
            cmd = xml.attributes().value("cmd").toString();
            data = xml.attributes().value("data").toString();
        }
        else
        {
            xml.skipCurrentElement();
        }
    }

    if (cmd == "sendCom")
    {
        widget->createButton(label, define, posX, posY,
                SLOT(sendCom(QString)), data);
    }
    else if (cmd == "setDef")
    {
        widget->createButton(label, define, posX, posY,
                SLOT(setDef(QString)), data);
    }
}

Solution

  • You need to use xml.skipCurrentElement(); after readButton(widget); and readCheckBox(widget);. Without that xml.readNextStartElement() reads end of element and breaks the loop.

    void XmlReader::readModule()
    {
        Q_ASSERT(xml.isStartElement() && xml.name() == "module");
    
        while (xml.readNextStartElement())
        {
            if (xml.name() == "button")
            {
                readButton(widget);
                xml.skipCurrentElement();
            }
            else if (xml.name() == "checkbox")
            {
                readCheckBox(widget);
                xml.skipCurrentElement();
            }
            else
            {
                xml.skipCurrentElement();
            }
        }
    }