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. How to save QMap
Forum Update on Monday, May 27th 2025

How to save QMap

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 711 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.
  • naaxN Offline
    naaxN Offline
    naax
    wrote on last edited by
    #1

    Hello guys, can u show me how I can save QMap using QFile?

    I'm trying to use keys and values, but I'm doing this wrong.

    QFile file(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "test.txt");
        if (file.open(QIODevice::WriteOnly | QIODevice::Text))
        {
                QTextStream stream(&file);
               for (int i=0;i<DailyTaskMap.count();i++)
               {
                   //stream << DailyTaskMap.key() <<"\n"; 
                   //stream << DailyTaskMap.value() <<"\n";
                   qDebug() << "SAVED";
               }
        }
        else
            qDebug() <<file.errorString();
    
    jsulmJ 1 Reply Last reply
    0
    • naaxN naax

      @jsulm

      I will use .txt file.

      DailyTaskMap[i].key() ?
      

      I tried this. I don't know how I can write this code correctly.

      ss.png

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @naax said in How to save QMap:

      I will use .txt file

      Does not answer my question: how will the file content look like?

      Please read documentation to see how to iterate over a QMap (https://doc.qt.io/qt-5/qmap.html):

      QMap<QString, int>::const_iterator i = map.constBegin();
      while (i != map.constEnd()) {
          cout << i.key() << ": " << i.value() << Qt::endl;
          ++i;
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      naaxN 1 Reply Last reply
      2
      • naaxN naax

        Hello guys, can u show me how I can save QMap using QFile?

        I'm trying to use keys and values, but I'm doing this wrong.

        QFile file(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "test.txt");
            if (file.open(QIODevice::WriteOnly | QIODevice::Text))
            {
                    QTextStream stream(&file);
                   for (int i=0;i<DailyTaskMap.count();i++)
                   {
                       //stream << DailyTaskMap.key() <<"\n"; 
                       //stream << DailyTaskMap.value() <<"\n";
                       qDebug() << "SAVED";
                   }
            }
            else
                qDebug() <<file.errorString();
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @naax What should be the format? This is something you need to decide bafore implementing anything? So, how should the file look like after saving?
        And shouldn't this DailyTaskMap.key() be

        DailyTaskMap[i].key()
        

        ?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        naaxN 1 Reply Last reply
        0
        • jsulmJ jsulm

          @naax What should be the format? This is something you need to decide bafore implementing anything? So, how should the file look like after saving?
          And shouldn't this DailyTaskMap.key() be

          DailyTaskMap[i].key()
          

          ?

          naaxN Offline
          naaxN Offline
          naax
          wrote on last edited by
          #3

          @jsulm

          I will use .txt file.

          DailyTaskMap[i].key() ?
          

          I tried this. I don't know how I can write this code correctly.

          ss.png

          jsulmJ 1 Reply Last reply
          0
          • naaxN naax

            @jsulm

            I will use .txt file.

            DailyTaskMap[i].key() ?
            

            I tried this. I don't know how I can write this code correctly.

            ss.png

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @naax said in How to save QMap:

            I will use .txt file

            Does not answer my question: how will the file content look like?

            Please read documentation to see how to iterate over a QMap (https://doc.qt.io/qt-5/qmap.html):

            QMap<QString, int>::const_iterator i = map.constBegin();
            while (i != map.constEnd()) {
                cout << i.key() << ": " << i.value() << Qt::endl;
                ++i;
            }
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            naaxN 1 Reply Last reply
            2
            • jsulmJ jsulm

              @naax said in How to save QMap:

              I will use .txt file

              Does not answer my question: how will the file content look like?

              Please read documentation to see how to iterate over a QMap (https://doc.qt.io/qt-5/qmap.html):

              QMap<QString, int>::const_iterator i = map.constBegin();
              while (i != map.constEnd()) {
                  cout << i.key() << ": " << i.value() << Qt::endl;
                  ++i;
              }
              
              naaxN Offline
              naaxN Offline
              naax
              wrote on last edited by
              #5

              @jsulm

              Thanks, bro a lot. Have a nice day!

              1 Reply Last reply
              0
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #6

                The more general term for this is object serialization: translating the object into a well defined format that can be saved to disk or transfered across a network. I'd advise that instead of reinventing and coming up with a custom format. You should review existing formats like XML or JSON, and if appropriate, then acquire or write translators to go between native format and the "serialized" format. Qt already has some serialization capabilities but I don't use them so I'm not sure what specifically exists.

                1 Reply Last reply
                3

                • Login

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