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. QMap fo QMap
Qt 6.11 is out! See what's new in the release blog

QMap fo QMap

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 3.1k Views 4 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.
  • JohanSoloJ Offline
    JohanSoloJ Offline
    JohanSolo
    wrote on last edited by JohanSolo
    #2

    Hello,

    First you need to have a QMap "value":

    QMap< QString, QVariant > myValueMap;
    myValueMap.insert( "the answer",  42 );
    myValueMap[ "my favourite C++ framework" ] = "Qt";
    // ...
    

    Then you can populate your data QMap:

    data.insert( "key", myValueMap );
    

    `They did not know it was impossible, so they did it.'
    -- Mark Twain

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

      Hi,

      QMap<QString, QVariant> entry;
      entry.insert(QStringLiteral("MyCoolKey"), 42);
      data.insert(QStringLiteral("MySecondCoolKey"), entry);
      

      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
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #4
        • QMap allows initializer list: data.insert(QStringLiteral("MySecondCoolKey"),QMap<QString, QVariant>({std::pair<QString, QVariant>(QStringLiteral("MyCoolKey"),42)}));
        • QMap operator[] creates the element if it doesn't exist: data[QStringLiteral("MySecondCoolKey")][QStringLiteral("MyCoolKey")] = QVariant::fromValue(42);

        P.S.
        Thinking of efficiency now, generally speaking, a container of containers can be badly inefficient. fortunately Qt containers were designed to minimise this inefficiency and C++11 move constructors made it much better even with std containers. While your code is ok as Qt took upon themselves to do the hard work of making it efficient, in general if you have to store a big object as value in a container store it as a pointer:

        • No QMap<int,MyBigObject> data; data.insert(42,MyBigObject());
        • Yes but you need to remember to free the memory QMap<int,MyBigObject*> data; data.insert(42,new MyBigObject());
        • Yes QMap<int, std::shared_ptr<MyBigObject> > data; data.insert(42,std::make_shared<MyBigObject>());

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3
        • C Offline
          C Offline
          Chobin
          wrote on last edited by
          #5

          Hi,

          Thanks for your answers.

          My goal is to write 300 parameters in a file. I'm looking to use this template

          QMap<QString,QMap<QString,QVariant> > data;

          because I'd like to write the file using QDataStream and the following operators:

          friend QDataStream & operator<<(QDataStream& str, const QMap<QString,QMap<QString,QVariant> > & fileStruct);
          friend QDataStream & operator>>(QDataStream& str, QMap<QString,QMap<QString,QVariant> > & fileStruct);
          

          Do you know a different way to reach my goal?

          Many thanks for your answers.

          Best Regards,

          Chobin

          VRoninV 1 Reply Last reply
          0
          • C Chobin

            Hi,

            Thanks for your answers.

            My goal is to write 300 parameters in a file. I'm looking to use this template

            QMap<QString,QMap<QString,QVariant> > data;

            because I'd like to write the file using QDataStream and the following operators:

            friend QDataStream & operator<<(QDataStream& str, const QMap<QString,QMap<QString,QVariant> > & fileStruct);
            friend QDataStream & operator>>(QDataStream& str, QMap<QString,QMap<QString,QVariant> > & fileStruct);
            

            Do you know a different way to reach my goal?

            Many thanks for your answers.

            Best Regards,

            Chobin

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #6

            @Chobin said in QMap fo QMap:

            because I'd like to write the file using QDataStream and the following operators:

            Since you are using Qt Classes you don't need to write your own operators, Qt already have them implemented.

            On the other hand if you are building a massive QMap just to save it to file there is a more efficient way, yes. If you show us your code we might be more specific.

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chobin
              wrote on last edited by kshegunov
              #7

              Hi,

              I have to write a file like this:

              Phase 1:
              parameter1 20
              parameter2 300
              parameter3 350
              Phase 2:
              parameter1 20
              parameter2 300
              parameter3 350
              Phase 3:
              parameter1 20
              parameter2 300
              parameter3 350
              Phase 4:
              parameter1 20
              parameter2 300
              parameter3 350
              Phase 5:
              parameter1 20
              parameter2 300
              parameter3 350
              ....

              As you know, I thought to use a QMap where the first key is "Phase1" and the second map key is the name of the parameters.

              QMap<QString,QMap<QString,QVariant> > fileStr;
              
              void readWriteFile::InitMap() {
                  fileStr["Phase1"]["Par1"] = 1;
                  fileStr["Phase1"]["Par2"] = 2;
                  fileStr["Phase1"]["Par3"] = 3;
              
                  fileStr["Phase2"]["Par1"] = 1;
                  fileStr["Phase2"]["Par2"] = 2;
                  fileStr["Phase2"]["Par3"] = 3;
              
                  fileStr["Phase3"]["Par1"] = 1;
                  fileStr["Phase3"]["Par2"] = 2;
                  fileStr["Phase3"]["Par3"] = 3;
              }
              
              Read/Write file methods:
              
              void readWriteFile::writeFile()
              {
              QFile file( filePath + "File.dat" );
              
              if ( file.open(QIODevice::WriteOnly) )
                  {
                      QDataStream out( &file );
                      out.setVersion(QDataStream::Qt_5_8);
                      out << fileStr;
                  }
              
                  file.close();
              }
              
              void readWriteFile::readFile()
              {
              QFile file( filePath + "File.dat");
              
                  if ( file.open(QIODevice::ReadOnly) )
                  {
                          QDataStream in( &file );
                          in.setVersion(QDataStream::Qt_5_8);
                          //Valuto se il file letto รจ corrotto
                          if (in.status() == QDataStream::ReadCorruptData)
                              qDebug() << "File corrupt";
                          else {
                              in >> fileStrRd;
                              qDebug() << "File OK";
                          }
                  }
              
                  file.close();
              }
              

              How can I struct my code?

              Many Thanks.

              Best Regards,

              Chobin

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #8

                Where do you currently store the number of phases, how many parameters each phase has and what are the parameters values?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Chobin
                  wrote on last edited by
                  #9

                  Hi,

                  the total phase number is 20 and each phase has 20 parameters. The value of these parameters can be an integer or a string.
                  Each phase has the same struct of the other phases.
                  I could not store the phase number in the file if I structure the file like I showed un the post above. My idea is to fill a struct or a Qt template when I read the file. The structure of these type of data is the same of the file....this is my idea.

                  What do you suggest?

                  Best Regards,

                  Chobin

                  the_T 1 Reply Last reply
                  0
                  • C Chobin

                    Hi,

                    the total phase number is 20 and each phase has 20 parameters. The value of these parameters can be an integer or a string.
                    Each phase has the same struct of the other phases.
                    I could not store the phase number in the file if I structure the file like I showed un the post above. My idea is to fill a struct or a Qt template when I read the file. The structure of these type of data is the same of the file....this is my idea.

                    What do you suggest?

                    Best Regards,

                    Chobin

                    the_T Offline
                    the_T Offline
                    the_
                    wrote on last edited by
                    #10

                    @Chobin
                    Would a json object be an option?

                    -- No support in PM --

                    1 Reply Last reply
                    1
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #11

                      What I would do is use QStandardItemModel + TreeView.

                      you can serialise the model easily in multiple formats with this: https://github.com/VSRonin/Qt-Model-Serialisation/tree/dev (make sure to use the dev branch), there is an example in the readme that should give you a fair idea of how to structure your model.

                      Since you then have to use it in QML you might want to use something like this: http://pastebin.com/70KjfZzm to overcome the annoying different treatment of multi-columns models in QML compared to QtWidgets


                      additional information on the problem was given via private chat in native language

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      1

                      • Login

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