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. QMultiMap with multiple key/value pairs
Forum Updated to NodeBB v4.3 + New Features

QMultiMap with multiple key/value pairs

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 514 Views 3 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.
  • M Offline
    M Offline
    Marcus Barnet
    wrote on last edited by Marcus Barnet
    #1

    I'm using QT Creator with QT Libraries and I have some combobox that I use to store QString values in a hierarchical model.

    Tipically, the values can be:

    KeyA -> value1A -> Key1A -> value2A && Key2A -> Value3A

    KeyA -> value4A -> Key2A -> value6A && key3A -> Value5A

    KeyB -> value1B -> Key1B -> value2B && Key2B -> Value3B

    where KeyA is the main key and it is associated to value1A and also to value4A. Moreover, the pair (KeyA,value1A) is also associated to Key1A and Key2A with their values.

    By looking for KeyA, I should be able to retrieve all the associated keys/values.

    I'm trying to use a QMultiMap and in this way I can associate a KEY to multiple VALUES. The problem is that I cannot find a solution to associate a main KEY to multiple keys/values.

    Should I use multiple QMultiMaps?

    I need to store all these data in a data structure since then I have to store them in a SQL database.

    EDIT1:

    I changed my code in order to use QMultiMap as:

     struct Values
        {
            QString nome;
            QString serie;
            QString ripetizioni;        
        };
        
        QMultiMap<QString, Values> scheda;
    

    but now, how can I insert new values into QMultiMap?

    scheda.insert("keyA", "value associated to *nome* in struct *Values*");
    
    Pl45m4P 1 Reply Last reply
    -1
    • M Marcus Barnet

      I'm using QT Creator with QT Libraries and I have some combobox that I use to store QString values in a hierarchical model.

      Tipically, the values can be:

      KeyA -> value1A -> Key1A -> value2A && Key2A -> Value3A

      KeyA -> value4A -> Key2A -> value6A && key3A -> Value5A

      KeyB -> value1B -> Key1B -> value2B && Key2B -> Value3B

      where KeyA is the main key and it is associated to value1A and also to value4A. Moreover, the pair (KeyA,value1A) is also associated to Key1A and Key2A with their values.

      By looking for KeyA, I should be able to retrieve all the associated keys/values.

      I'm trying to use a QMultiMap and in this way I can associate a KEY to multiple VALUES. The problem is that I cannot find a solution to associate a main KEY to multiple keys/values.

      Should I use multiple QMultiMaps?

      I need to store all these data in a data structure since then I have to store them in a SQL database.

      EDIT1:

      I changed my code in order to use QMultiMap as:

       struct Values
          {
              QString nome;
              QString serie;
              QString ripetizioni;        
          };
          
          QMultiMap<QString, Values> scheda;
      

      but now, how can I insert new values into QMultiMap?

      scheda.insert("keyA", "value associated to *nome* in struct *Values*");
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

      I need to store all these data in a data structure since then I have to store them in a SQL database.

      Why you need to store them in a structure when you have your database?
      Why not add your data to database directly?

      @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

      how can I insert new values into QMultiMap?

      https://doc.qt.io/qt-5/qmultimap.html#details


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      M 1 Reply Last reply
      2
      • M Offline
        M Offline
        Marcus Barnet
        wrote on last edited by Marcus Barnet
        #3

        The problem is that I need to check if a pair of Key, Value is already present in the QMultiMap and if yes, I do not have to add these values to the combobox again since this generates duplicates.

        void Pump::on_combo_gruppo_muscolare1_currentIndexChanged(const QString &arg1)
        {
            esercizio *ptr;
            qDebug() << "Carico gli esercizi" << arg1;
            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
            db.setHostName("localhost");
            db.setDatabaseName("pump_db");
            db.setUserName("root");
            db.setPassword("root");
            bool ok = db.open();
        
            if(ok){
        
               QSqlQueryModel model;
               model.setQuery("SELECT * FROM esercizio WHERE gruppo_muscolare = '" + arg1 + "'");
        
        
        
               for (int i = 0; i < model.rowCount(); ++i) {
        
                   if (scheda.contains(model.record(i).value("gruppo_muscolare").toString(), model.record(i).value("nome").toString())){
                            qDebug() << "contiene già: " << model.record(i).value("gruppo_muscolare").toString();
                            break;
                           }
                           else {
                           scheda.insert(arg1, {model.record(i).value("nome").toString(), "", ""});
                           ui->comboBox_2->addItem(model.record(i).value("nome").toString());
                           ui->comboBox_8->addItem(model.record(i).value("nome").toString());
                           ui->comboBox_3->addItem(model.record(i).value("nome").toString());
                           ui->comboBox_9->addItem(model.record(i).value("nome").toString());
        }
        
                   }
               }
        
        }
        

        The error is at this line:

        if (scheda.contains(model.record(i).value("gruppo_muscolare").toString(), model.record(i).value("nome").toString())){
        

        because I don't know how to correctly pass the second parameter to "bool contains(const Key &key, const T &value) const" function since I'm using a structure as second argument in the QMultiMap.

        What should I do in order to solve this problem?

        EDIT: "gruppo_muscolare" is the KEY and "nome" is the value in the QMultiMap. The same KEY can have multiple VALUES.

        1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

          I need to store all these data in a data structure since then I have to store them in a SQL database.

          Why you need to store them in a structure when you have your database?
          Why not add your data to database directly?

          @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

          how can I insert new values into QMultiMap?

          https://doc.qt.io/qt-5/qmultimap.html#details

          M Offline
          M Offline
          Marcus Barnet
          wrote on last edited by
          #4

          @Pl45m4 said in QMultiMap with multiple key/value pairs:

          @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

          I need to store all these data in a data structure since then I have to store them in a SQL database.

          Why you need to store them in a structure when you have your database?
          Why not add your data to database directly?

          @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

          how can I insert new values into QMultiMap?

          https://doc.qt.io/qt-5/qmultimap.html#details
          Because I need to apply some modifications to the data before to store them into the database.

          However, my question wasn't on how to insert standard values in QMultiMap, but it was about the specific case where QMultiMap is used with a structure.

          However, I solved it and now everything works fine.

          Pablo J. RoginaP 1 Reply Last reply
          0
          • M Marcus Barnet

            @Pl45m4 said in QMultiMap with multiple key/value pairs:

            @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

            I need to store all these data in a data structure since then I have to store them in a SQL database.

            Why you need to store them in a structure when you have your database?
            Why not add your data to database directly?

            @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

            how can I insert new values into QMultiMap?

            https://doc.qt.io/qt-5/qmultimap.html#details
            Because I need to apply some modifications to the data before to store them into the database.

            However, my question wasn't on how to insert standard values in QMultiMap, but it was about the specific case where QMultiMap is used with a structure.

            However, I solved it and now everything works fine.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @Marcus-Barnet said in QMultiMap with multiple key/value pairs:

            I solved it and now everything works fine.

            Please don't forget to mark your post as solved!

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            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