我正在尝试使用 QXmlStreamReader 读取 XML 文档。当我尝试将指向读者的指针传递给其他类时,我遇到了问题。我在一个类中创建了一个阅读器实例。该类读取 XML,直到遇到定义新类的块。我创建了新类的实例,然后调用该类中的一个函数以继续读取特定于它的 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 用来识别通道和伺服模块的代码。我还添加了 ChannelClass 和 ServoClass 的代码。最后,我添加了我的 XML 文件的编辑版本,但足以显示问题。这个 XML 可以很好地读入我的 TreeWidget 代码(未显示)。当我将它读入上面的代码时发生的情况是所有通道都被正确读取,然后它读取第一个伺服块并一直返回到主 ModeClass 循环,而不是读取 ModeClass::ReadServos 中的下一个伺服。我知道这是很多代码,但它似乎是显示问题的唯一方法。谢谢,