gpt4 book ai didi

c++ - 将 QXmlStreamReader 实例传递给类

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:13 26 4
gpt4 key购买 nike

我正在尝试使用 QXmlStreamReader 读取 XML 文档。当我尝试将指向读者的指针传递给其他类时,我遇到了问题。我在一个类中创建了一个阅读器实例。该类读取 XML,直到它到达定义新类的 block 。我创建新类的实例,然后调用该类中的函数以继续读取特定于它的 XML。例如:

void SF_UnitClass::ReadModes()
{
Q_ASSERT(XML.isStartElement() && XML.name() == MODES);
NumModes = XML.attributes().value(COUNT).toInt();
while (XML.readNextStartElement())
{
if (XML.name() == MODE)
{
ModeClass* pMode = new ModeClass(this);
ModeList += XML.attributes().value(ID).toString();
pMode->ReadXML(&XML);
{
}
}
else
{
XML.raiseError(QObject::tr("Something other than Mode block encountered in Modes block"));
}
}
}

void ModeClass::ReadXML(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == MODE);
while (pXML->readNextStartElement())
{
int nameIndex = ModeList.indexOf(pXML->name().toString());
if(nameIndex != -1)
{
switch(nameIndex)
{
case 0:
{
qDebug() << "Mode: Receivers";
NumReceivers = pXML->readElementText().toInt();
break;
}
case 1:
{
qDebug() << "Mode: Channels";
ReadChannels(pXML);
break;
}
case 2:
{
qDebug() << "Mode: Servos";
ReadServos(pXML);
break;
}
}
}
else
{
qDebug() << "Mode: Error " << name;
pXML->raiseError(QObject::tr("Unrecognized keyword for Mode"));
}
}
}

void ModeClass::ReadChannels(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == CHANNELS);
NumChannels = pXML->attributes().value(COUNT).toInt();
while (pXML->readNextStartElement())
{
if (pXML->name() == CHANNEL)
{
qDebug() << "Mode: Read Channel";
ChannelClass* pChannel = new ChannelClass(this);
pChannel->ReadXML(pXML);
}
else
{
pXML->raiseError(QObject::tr("Something other than Channel block enc
}
}
}

void ModeClass::ReadServos(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == SERVOS);
NumServos = pXML->attributes().value(COUNT).toInt();
while (pXML->readNextStartElement())
{
if (pXML->name() == SERVO)
{
qDebug() << "Mode: Read Servo";
ServoClass* pServo = new ServoClass(this);
pServo->ReadXML(pXML);
}
else
{
qDebug() << "Servos: raiseError";
pXML->raiseError(QObject::tr("Something other than Servo block encou
}
}
qDebug() << "Mode: Read Servos Exit";
}

void ChannelClass::ReadXML(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == CHANNEL);
ChannelNumber = pXML->attributes().value(ID).toInt();
while (pXML->readNextStartElement())
{
int nameIndex = ChannelList.indexOf(pXML->name().toString());
if(nameIndex != -1)
{
switch(nameIndex)
{
case 0:
{
UserName = pXML->readElementText();
break;
}
case 1:
{
EndPointHold = String2Bool(pXML->readElementText());
break;
}
case 2:
{
ServoPriority = String2Bool(pXML->readElementText());
break;
}
}
}
else
{
pXML->raiseError(QObject::tr("Unrecognized keyword for Channel"));
}
}
}

void ServoClass::ReadXML(QXmlStreamReader* pXML)
{
Q_ASSERT(pXML->isStartElement() && pXML->name() == SERVO);
ServoNumber = pXML->attributes().value(ID).toInt();
while (pXML->readNextStartElement())
{
int nameIndex = ServoList.indexOf(pXML->name().toString());
qDebug() << "Servo: NameIndex: " << nameIndex;

if(nameIndex != -1)
{
switch(nameIndex)
{
case 0:
{
Offset = pXML->readElementText().toInt();
break;
}
case 1:
{
PosFactor = pXML->readElementText().toInt();
break;
}
case 2:
{
NegFactor = pXML->readElementText().toInt();
break;
}
case 3:
{
SecServo = pXML->readElementText().toInt();
break;
}
case 4:
{
Unit = pXML->readElementText().toInt();
break;
}
case 5:
{
Ser1 = pXML->readElementText().toInt();
break;
}
case 6:
{
Ser2 = pXML->readElementText().toInt();
break;
}
case 7:
{
Ser3 = pXML->readElementText().toInt();
break;
}
}
}
else
{
qDebug() << "Servo: raiseError";
pXML->raiseError(QObject::tr("Unrecognized keyword for Servo block")
}
}
qDebug() << "Servo: Exit ";
}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<SmartFlyUnit version="1.0">
<UnitName>PowerExpander Eq10E</UnitName>
<UnitCode>PE-5</UnitCode>
<UnitID>1</UnitID>
<MinRev>1.0</MinRev>
<MaxRev>1.9</MaxRev>
<Servos>36</Servos>
<Outputs>32</Outputs>
<UnitMode>Chan</UnitMode>
<Modes Count="2">
<Mode ID="Chan">
<Receivers>1</Receivers>
<Channels Count="10">
<Channel ID="0">
<UserName></UserName>
<EndPtHold>Off</EndPtHold>
<ServoPri>Off</ServoPri>
</Channel>
<Channel ID="1">
<UserName></UserName>
<EndPtHold>Off</EndPtHold>
<ServoPri>Off</ServoPri>
</Channel>
</Channels>
<Servos Count="36">
<Servo ID="0">
<Offset>0</Offset>
<PosFact>1.0000</PosFact>
<NegFact>1.0000</NegFact>
<Reverse>Off</Reverse>
<SecServo>0xFF</SecServo>
<Unit>0xFF</Unit>
<Ser1>0xFF</Ser1>
<Ser2>0xFF</Ser2>
<Ser3>0xFF</Ser3>
</Servo>
<Servo ID="1">
<Offset>0</Offset>
<PosFact>1.0000</PosFact>
<NegFact>1.0000</NegFact>
<Reverse>Off</Reverse>
<SecServo>0xFF</SecServo>
<Unit>0xFF</Unit>
<Ser1>0xFF</Ser1>
<Ser2>0xFF</Ser2>
<Ser3>0xFF</Ser3>
</Servo>
</Servos>
</Mode>
</Modes>
<MCUs Count="1">
<MCU ID="0">
<FileName>/PE-5/PE-5_M0_1_00.sffw</FileName>
</MCU>
</MCUs>
<FPGAs Count="1">
<FPGA ID="0">
<Configs Count="2">
<Config ID="0">
<FileName>/PE-5/PE-5_F0C0_1_00.sffw</FileName>
</Config>
<Config ID="1">
<FileName>/PE-5/PE-5_F0C1_1_00.sffw</FileName>
</Config>
</Configs>
</FPGA>
</FPGAs>
</SmartFlyUnit>

我添加了 ModeClass 用来识别 channel 和伺服模块的代码。我还添加了来自 ChannelClass 和 ServoClass 的代码。最后我添加了我的 XML 文件的编辑版本但足以显示问题。此 XML 可以很好地读入我的 TreeWidget 代码(未显示)。当我将它读入上面的代码时会发生什么,所有 channel 都被正确读取,然后它读取第一个伺服 block 并一直返回到主 ModeClass 循环,而不是读取 ModeClass::ReadServos 中的下一个伺服。我知道这是很多代码,但它似乎是显示问题的唯一方法。谢谢,

最佳答案

Qt 文档说:

Reads until the next start element within the current element. Returns true when a start element was reached. When the end element was reached, or when an error occurred, false is returned.

所以遇到start元素时,使用readNextStartElement()将在您找到的元素内搜索

<xml>
</xml>
<xml>
</xml>

这里会发生什么:

  1. readNextStartElement()将首先停止 <xml>节点
  2. readNextStartElement()将返回 false,因为它发现 </xml>

稍微修改一下示例:

<xml>
<child>
</child>
</xml>
<xml>
</xml>
  1. readNextStartElement()将首先停止 <xml>节点
  2. readNextStartElement()将停止 <child>节点
  3. readNextStartElement()将返回 false,因为 <child> 中没有 xml 节点节点

你问我该怎么办?

在您到达您感兴趣的节点并阅读您需要的所有信息之后,您必须调用 skipCurrentElement()所以QXmlStreamReader将转到当前元素的末尾

所以修改一点第一个示例:

<xml>
</xml>
<xml>
</xml>
  1. readNextStartElement()将首先停止 <xml>节点
  2. skipCurrentElement()将在第一个 <xml> 关闭时停止节点
  3. readNextStartElement()将在秒 <xml> 停止节点

关于c++ - 将 QXmlStreamReader 实例传递给类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com