Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

QMultiMap with multiple key/value pairs



  • 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*");


  • @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



  • 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.



  • @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.



  • @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!


Log in to reply