Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Opening XML files

[SOLVED] Opening XML files

Scheduled Pinned Locked Moved General and Desktop
qxmlqfile
4 Posts 2 Posters 2.8k Views
  • 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.
  • J Offline
    J Offline
    JonBriem
    wrote on 12 Jul 2015, 22:21 last edited by SGaist
    #1

    Hi!

    I'm currently creating a simple game using Qt and I want to have a highscores feature in my game. For that I was going to use an XML file, but I'm having problems with the code. My code looks like this:

    QFile highscores(":/statistics/resources/highscores.xml");
        if(highscores.exists())qDebug() << "File exists";
        QDomDocument document;
        QDomElement root = document.createElement("Ranks");
        document.appendChild(root);
        for(int i = 0; i < 10; i++)
        {
            QDomElement entry = document.createElement("Entry");
            entry.setAttribute("Name", "");
            entry.setAttribute("Rank", QString::number(i+1));
            entry.setAttribute("Score",QString::number(0));
            root.appendChild(entry);
        }
        if(!highscores.open(QIODevice::WriteOnly | QIODevice::Text))
        {
            qDebug() << "Failed to open file for writing";
        }
        else
        {
            QTextStream stream(&highscores);
            stream << document.toString();
            highscores.close();
            qDebug() << "Finished";
        }
    

    When I run the program the output is
    "File exists"
    "Failed to open file for writing"

    Can anyone explain why I can't seem to open the file?

    cheers,
    Jon

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 12 Jul 2015, 22:30 last edited by mrjj 7 Dec 2015, 22:43
      #2

      @JonBriem said:

      QFile highscores(":/statistics/resources/highscores.xml");
          if(!highscores.open(QIODevice::WriteOnly | QIODevice::Text))
          {
          qDebug() << "Failed to open file for writing";
          }
      

      is that path inside the resource file? (which is linked into the exe)

      changing the path to
      QFile highscores ( "highscores.xml" );
      the code below did produce the expected xml file.

      QDomDocument document;
      
      void CreateRanks()
      {
      
          QDomElement root = document.createElement ( "Ranks" );
          document.appendChild ( root );
      
          for ( int i = 0; i < 10; i++ ) {
              QDomElement entry = document.createElement ( "Entry" );
              entry.setAttribute ( "Name", "" );
              entry.setAttribute ( "Rank", QString::number ( i + 1 ) );
              entry.setAttribute ( "Score", QString::number ( 0 ) );
              root.appendChild ( entry );
          }
      }
      
      void SaveRanks()
      {
          QFile highscores ( "highscores.xml" );
          if ( !highscores.open ( QIODevice::WriteOnly | QIODevice::Text ) ) {
              qDebug() << "Failed to open file for writing";
          } else {
              QTextStream stream ( &highscores );
              stream << document.toString();
              highscores.close();
              qDebug() << "Finished";
          }
      }
      
      int main ( int argc, char* argv[] )
      {
      CreateRanks();
      SaveRanks();
      }
      
      1 Reply Last reply
      1
      • J Offline
        J Offline
        JonBriem
        wrote on 12 Jul 2015, 22:54 last edited by
        #3

        That worked, thank you so much :)

        M 1 Reply Last reply 13 Jul 2015, 09:10
        0
        • J JonBriem
          12 Jul 2015, 22:54

          That worked, thank you so much :)

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 13 Jul 2015, 09:10 last edited by
          #4

          @JonBriem
          Most welcome. If possible,please edit the first post you made and add [SOLVED] in front of title.

          1 Reply Last reply
          0

          3/4

          12 Jul 2015, 22:54

          • Login

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