How to Read and Write in XML Files with Qt/C++
In this post I will show an basic example of a Qt application which uses QXmlStreamReader and QXmlStreamWriter objects to write and read XML files . I will also show how to change icon of QPushbutton according to its state (Unchecked or Checked).
WRITING XML:
When we selected the fields with the informations and clicked on FileMenu->SaveAs the function SaveXMLFIle() is called . Then, a dialog will request for to save the filename. Once the XML is created and is showed on QTextEdit.
void XmlUpnp::SaveXMLFile()
{
filename = QFileDialog::getSaveFileName(this,
tr("Save Xml"), ".",
tr("Xml files (*.xml)"));
QFile file(filename);
file.open(QIODevice::WriteOnly);
QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("LAMPS");
xmlWriter.writeStartElement("LIGHT1");
xmlWriter.writeTextElement("State", ui.pushButton1->isChecked()?"Off":"On" );
xmlWriter.writeTextElement("Room",ui.comboBox1->currentText());
xmlWriter.writeTextElement("Potencial",QString::number(ui.spinBox1->value()));
xmlWriter.writeEndElement();
...
xmlWriter.writeStartElement("LIGHT4");
xmlWriter.writeTextElement("State", ui.pushButton4->isChecked()?"Off":"On" );
xmlWriter.writeTextElement("Room",ui.comboBox4->currentText());
xmlWriter.writeTextElement("Potencial",QString::number(ui.spinBox4->value()));
xmlWriter.writeEndElement();
xmlWriter.writeEndElement();
xmlWriter.writeEndDocument();
file.close();
ShowXmlOnScreen();
statusBar()->showMessage(tr("Xml Saved"));
}
READING XML:
When we clicked on FileMenu->Open the function OpenXMLofFIle() is called . Is opened a QFileDialog and the file is choosen. After the the XML is showed on QTextEdit and the others fields are filled.
void XmlUpnp::OpenXMLofFile()
{
ui.textEdit->clear();
filename = QFileDialog::getOpenFileName(this,
tr("Open Xml"), ".",
tr("Xml files (*.xml)"));
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
std::cerr << "Error: Cannot read file " << qPrintable(filename)
<< ": " << qPrintable(file.errorString())
<< std::endl;
}
Rxml.setDevice(&file);
Rxml.readNext();
while(!Rxml.atEnd())
{
if(Rxml.isStartElement())
{
if(Rxml.name() == "LAMPS")
{
READNEXT();
}
else if(Rxml.name() == "LIGHT1")
{
ReadLightElement(0);//entra
}
else if(Rxml.name() == "LIGHT2")
{
ReadLightElement(1);//entra
}
else if(Rxml.name() == "LIGHT3")
{
ReadLightElement(2);//entra
}
else if(Rxml.name() == "LIGHT4")
{
ReadLightElement(3);//entra
}
else
{
Rxml.raiseError(QObject::tr("Not a bookindex file"));
}
}
else
{
READNEXT();
}
}
file.close();
if (Rxml.hasError())
{
std::cerr << "Error: Failed to parse file "
<< qPrintable(filename) << ": "
<< qPrintable(Rxml.errorString()) << std::endl;
}
else if (file.error() != QFile::NoError)
{
std::cerr << "Error: Cannot read file " << qPrintable(filename)
<< ": " << qPrintable(file.errorString())
<< std::endl;
}
ShowXmlOnScreen();
statusBar()->showMessage(tr("Xml Opened"));
}
CHANGING ICONS OF BUTTONS:
When the PushButton of the Lamp is clicked, its picture is changed according to state (ON or OFF).On the XmlUpnp class constructor is started two QIcons with two pictures. The first is a Off lamp and the second is a ON Lamp. At the moment of the click on the Button ,the on_pushButtonX_toggled() function is called changing the picture to according of the Lamp state , (Checked=Off or Unchecked=On).
XmlUpnp::XmlUpnp(QWidget *parent) : QMainWindow(parent)
{
...
icon1.addPixmap(QPixmap(QString::fromUtf8("icons/onlight1.png")),
QIcon::Normal,QIcon::Off);
icon2.addPixmap(QPixmap(QString::fromUtf8("icons/offlight1.png")),
QIcon::Normal,QIcon::Off);
}
void XmlUpnp::on_pushButton1_toggled()
{
if(ui.pushButton1->isChecked())
{
ui.pushButton1->setIcon(icon2);
labelArray[0]->setText(”OFF”);
}
else
{
ui.pushButton1->setIcon(icon1);
labelArray[0]->setText(”ON”);
}
}
SCREENS:
The start screen of application shows four pushbuttons and for each pushbutton, a Label with the state of the lamp, a QComboBox with the room where the lamp is , other QComboBox with the lamp potential and a QTextEdit with the XML code.
All fields are clean waiting the user change the value or open a XML file at FileMenu->Open .Then the XML file is showed on the QTextEdit and the others fields are filled with the information on the XML opened.
DOWNLOAD CODE:


Leave a comment