2

Update

void MainWindow::readXml()
{
    QDomDocument Champions;
    QFile xmlFile("champions.xml");
    xmlFile.open(QIODevice::ReadOnly);

    Champions.setContent(&xmlFile);

    QDomElement root = Champions.firstChildElement();
    QDomNodeList ChampionNames = root.elementsByTagName("Champion");

    for(int i = 0; i < ChampionNames.count(); i++)
    {
        QDomNode championNode = ChampionNames.at(i);
        if(championNode.isElement())
        {
            QDomElement Champion = championNode.toElement();
            ui->comboBox->addItem(Champion.attribute("Name"));
}}}

Managed to get something like this so I've the names in the comboBox now :)

I'm new to this community so I'm happy to meet all of You!

First I want to inform You that I'm pretty new to Qt programming however I've had some basic c++ lessons in school though it was only console programming and I've never worked on stuff like that. Why I've started with Qt? It looked easy to me and hell it was compared to visual studio! So here is my problem.

Basiclly I have a comboBox where I would read my "Name="" " attributes in. There will be around 100 maybe abit less maybe a bit more don't know Yet. I don't know how to start with everything this since I have never done such thing before. What i want the software to do is basiclly when i select a "name" in the combobox, i want all the attributes ("Q" "W" "E" "R") to be printed out in the 4 labels as You can see on the little image I've added.

I don't know if I need to first read the file into some strings arrays or data structures. Do I need to search the XML file for the "Name" which is selected in ComboBox and then somehow print it out? I spent some time on this but I cant find a way to achive the thing I want. I would really appriciate some code exemples specially using the ComboBox since as said I'm new to this.

XML File looks like this in case image is blurry:

<ROOT>

 <Champ Name="XXX1">
  <Q>QQ1</Q>
  <W>WW1</W>
  <E>EE1</E>
  <R>RR1</R>
 </Champ>

 <Champ Name="XXX2">
  <Q>QQ2</Q>
  <W>WW2</W>
  <E>EE2</E>
  <R>RR2</R>
 </Champ>

</ROOT>

I'm really bad on describing things so I've made a small ilustration using a pen to let You understand me better :)

My beautiful sketch.

Thanks for Your support in advance! Hope I'm clear enought with my question. Have a great day.

4

2 回答 2

1

首先,您应该将XML数据表示为 C++ 类/结构:

class Champ {
public:
   // A constructor using QDomElement as argument
   Champ(QDomElement element);
   QString name;
   QString q, w, e, r;
};

其次,您应该加载 XML 文件,对其进行解析,然后用对象填充矢量(或地图)Champ

QVector<Champ> loadChampsObjects(const QString& xmlPath)
{
    QVector<Champ> champObjects;
    QFile file(xmlPath);
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
        return champObjects;

    /* QDomDocument takes any QIODevice. as well as QString buffer*/
    QDomDocument doc("mydocument"); 
    if (!doc.setContent(file)) 
        return champObjects;

    //Get the root element
    QDomElement docElem = doc.documentElement(); 

    // get the nodes we need
    QDomNodeList nodeList = docElem.elementsByTagName("champ");

    // Check each node create a Champ object and add it the vector...
    for(int i = 0; i < nodeList.count(); i++)
        champObjects.append(Champ(nodeList.at(i).toElement()));

    return champObjects;
}

第三填充QComboBox. 我们将使用向量中的索引作为userData

QVector<Champ> champObjects = loadChampsObjects("path.to.xml");
for (unsigned i=0; i<champObjects.count(); i++)
    pComboBox->addItem(champObjects[i].name, QVariant(i));

最后,在连接到组合框信号的插槽中,您可以使用指示向量中索引的currentIndexChanged轻松访问所选对象的属性:userData

void champObjectChanged()
{
    unsigned vectorIndex = pComboBox->itemData(pComboBox->currentIndex())->toInt();
    Champ c = champObjects[vectorIndex];
    // do whatever you want with it
}
于 2013-01-09T12:09:37.017 回答
0

使用 Qt 的 DOM 组件为您完成所有解析和树构建(请参阅此处的示例以了解它们的使用)。

对于QComboBox,一旦QDomDocument完成,您可以搜索所有Champ节点并从中读取Name属性。对于每一个,只需使用QComboBox::addItem(const QString& text)添加它们。

然后将QComboBox::currentIndexChanged (const QString&)信号连接到 XML 处理类中的方法,该方法在 DOM 树中搜索具有与其匹配的属性的Champ节点。Name一旦找到读取它的每个子节点值(即“Q”、“W”、“E”、“R”值)。

于 2013-01-09T08:33:12.403 回答