Edit an XML file from Qt GUI



  • Hai everybody,
    I am a student and pretty new to Qt. I just want to make a GUI from which i can make changes to an XML file 'myData.xml' stored at "home/me/Qt/".

    <book>
    	<name>myBook</name>
           <finished>0</finished> <!-- 1 if finished, 0 if not -->
    </book>
    

    I have 2 check boxes (yes and No) in the GUI for the 'finished?' option. When i check the 'yes' box, the value 1 must be set and the file must be saved (overwritten) at the same place.
    Given below is the checkBox_pressed function:

    void MainWindow::on_checkBox_pressed()
    {
     
        QFile file("home/me/Qt/config.xml");
        if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
        {
            qDebug()<<"File not found";
        }
    else
        {
        QDomDocument document;
        document.setContent(&file);
        QDomElement root = document.documentElement();
        root.childNodes().at(2).setNodeValue("1");
    
        // Write changes to same file
        file.resize(0);
        QTextStream stream;
        stream.setDevice(&file);
        document.save(stream, 4);
        file.close();
    }
    }
    

    Am i missing something here? I doubt if i have provided the file path correctly.
    Please give me some valuable insights.


  • Lifetime Qt Champion

    Hi,

    Did you check the content of your QDomDocument instance after your modifications ?
    Are you sure that it's child node number two that you should be modifying ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.