Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Edit an XML file from Qt GUI
Forum Updated to NodeBB v4.3 + New Features

Edit an XML file from Qt GUI

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 825 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rahul_raj
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved