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 - Find all the keys in common with another key
QtWS25 Last Chance

QMultimap - Find all the keys in common with another key

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

    Hi to all,

    I'm using QMultimap to store some values in my application in this way:

    struct scheda
       {
           QString gruppo_muscolare;
           QString esercizio;
           QString serie;
           QString ripetizioni;
       };
       QMultiMap<QString, scheda> tabella;
    

    Then, I populate it by using a for loop after a SQL query:

    model.setQuery("...");
         for (int i = 0; i < model.rowCount(); ++i) {
    
          tabella.insert(model.record(i).value("scheda.tipo").toString(), {model.record(i).value("gruppo_muscolare.nome").toString(), model.record(i).value("esercizio.nome").toString(), model.record(i).value("esercizio_scheda.serie").toString(), model.record(i).value("esercizio_scheda.ripetizioni").toString()});
           }
    

    and I print all the keys associate to a specific key in this way:

    if(tabella.contains("A")){
    
             QMultiMap<QString, scheda>::iterator i = tabella.find("A");
                while (i != tabella.end() && i.key() == "A") {
                     qDebug() << "scheda tipo: " << i.key() << "gruppo muscolare: " << i->gruppo_muscolare << " - esercizio: " << i->esercizio  << " - serie: " << i->serie << " - rep: " << i->ripetizioni;
                 ++i;
             }
    

    and I get this result:

    scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "incline press hammer" serie: "1" ripetizioni: "2"
    scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "Chest Press " serie: "3" ripetizioni: "2"
    scheda tipo: "A" gruppo_muscolare: "quadricipiti" nome: "Leg Curl" serie: "2" ripetizioni: "3"

    I would like to group the results when gruppo_muscolare has the same value in order to trigger different actions.

    for example:

    if gruppo_muscolare == "pettorali"
    then do this
    else if gruppo_muscolare == "quadricipiti"
    then do this
    ..

    the problem is that I can't suppose to know what "gruppo_muscolare" will contain each time so I think I need to group all the keys which have in common the same value in "gruppo_muscolare".

    How can I do that?

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

      you need a map for each data field that you intend to use as a "key". IIUC you are asking for reverse lookup, where the data field becomes the key and the key becomes the data field. There may be a more elegant algorithmic solution, but given your description one is not immediately evident.

      Reading between the lines though, if you are aggregating this data from a SQL database then why not let the DB do the grunt work and concentrate on proper SQL queries to pull the information, grouped as you desire?

      1 Reply Last reply
      3
      • M Marcus Barnet

        Hi to all,

        I'm using QMultimap to store some values in my application in this way:

        struct scheda
           {
               QString gruppo_muscolare;
               QString esercizio;
               QString serie;
               QString ripetizioni;
           };
           QMultiMap<QString, scheda> tabella;
        

        Then, I populate it by using a for loop after a SQL query:

        model.setQuery("...");
             for (int i = 0; i < model.rowCount(); ++i) {
        
              tabella.insert(model.record(i).value("scheda.tipo").toString(), {model.record(i).value("gruppo_muscolare.nome").toString(), model.record(i).value("esercizio.nome").toString(), model.record(i).value("esercizio_scheda.serie").toString(), model.record(i).value("esercizio_scheda.ripetizioni").toString()});
               }
        

        and I print all the keys associate to a specific key in this way:

        if(tabella.contains("A")){
        
                 QMultiMap<QString, scheda>::iterator i = tabella.find("A");
                    while (i != tabella.end() && i.key() == "A") {
                         qDebug() << "scheda tipo: " << i.key() << "gruppo muscolare: " << i->gruppo_muscolare << " - esercizio: " << i->esercizio  << " - serie: " << i->serie << " - rep: " << i->ripetizioni;
                     ++i;
                 }
        

        and I get this result:

        scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "incline press hammer" serie: "1" ripetizioni: "2"
        scheda tipo: "A" gruppo_muscolare: "pettorali" nome: "Chest Press " serie: "3" ripetizioni: "2"
        scheda tipo: "A" gruppo_muscolare: "quadricipiti" nome: "Leg Curl" serie: "2" ripetizioni: "3"

        I would like to group the results when gruppo_muscolare has the same value in order to trigger different actions.

        for example:

        if gruppo_muscolare == "pettorali"
        then do this
        else if gruppo_muscolare == "quadricipiti"
        then do this
        ..

        the problem is that I can't suppose to know what "gruppo_muscolare" will contain each time so I think I need to group all the keys which have in common the same value in "gruppo_muscolare".

        How can I do that?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Marcus-Barnet
        As @Kent-Dorfman has said.

        You might choose to move the work to GROUP statements at the SQL database side.

        But if you do stick to your QMultiMap. You are perhaps already aware, but you can address your requirement via: ISTM that you need a new QMultiMap whose keys are the values you encounter in scheda.gruppo_muscolare. Populate that from the records you get back from your query. Now you can enumerate this new map's keys (e.g. in alphabetical order) to retrieve gruppo_muscolare grouped.

        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