Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to create a new XML file when edited in the UI?

How to create a new XML file when edited in the UI?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 2 Posters 1.4k 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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on last edited by
    #1

    I have a UI which parses a value from an XML file and this value appears in the QLineEdit.When user changes the lineEdit value this should be written in the new XML file.How do I do that?I'm parsing some 20 values from XML and displaying all 20 values in the UI lineEdits.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Use either QXmlStreamReader + QXmlStreamWriter or QDom.

      (Z(:^

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kishore_hemmady
        wrote on last edited by
        #3

        @sierdzio
        Hi
        I'm using QDom for reading.but how can i copy the complete original xml file except the edited value to the new file and the edited value should be updated in the new xml file.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          QDom holds the whole XML structure in memory. All that you need to do is to save it back to your file. Something like:

          QDomDocument dom; // Your doc
          QFile xmlFile("path/to/your/file.xml");
          if (xmlFile.open(QFile::WriteOnly | QFile::Text)) {
            xmlFile.write(dom.toString(4)); // Or toByteArray()
          }
          

          (Z(:^

          1 Reply Last reply
          2
          • K Offline
            K Offline
            kishore_hemmady
            wrote on last edited by
            #5

            @sierdzio
            Hi,
            I'm getting this error on using your snippet

            /home/kishor/mainwindow.cpp:346: error: no matching function for call to ‘QFile::write(QString)’
            xmlFile.write(document.toString(4)); // Or toByteArray()

            What is that 4 doing there?
            ^

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              Read the docs I've linked. 4 creates the file with 4 space indent. https://doc.qt.io/qt-5/qdomdocument.html#toString

              If that does not work, use the toByteArray(), like I've (also) said already.

              (Z(:^

              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