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 add an item to a QList inside a QMap?
QtWS25 Last Chance

How to add an item to a QList inside a QMap?

Scheduled Pinned Locked Moved Solved General and Desktop
qlistqmap
5 Posts 2 Posters 7.1k 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.
  • F Offline
    F Offline
    Ferni
    wrote on 30 May 2016, 18:53 last edited by
    #1

    Hi everyone,
    I'm designing a structure in my programa and I have something like this:

    QMultiMap<QString, QList<QString> > topicList;

    Every QString (key) should be a topic in my MQTT protocol and every QList (value) is a list of the Client's ID subscribed to the previous topic.

    I've read the documentation buy I can not understand how to add an element to my QList without overwriting anything else. I'd like to be able to search the IDs of the connected clients to a certain topic and add o remove IDs.
    Is this possible? What should I do?

    Many thanks,
    Fernando

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 30 May 2016, 19:58 last edited by Chris Kawa
      #2

      From what you describe you want a map, not a multimap:

      QMap<QString, QList<QString> > topicList;
      
      //add some IDs to a topic:
      topicList["foo"].append("bar");
      topicList["foo"].append("bazz");
      
      //find a list of IDs for a topic:
      auto it = topicList.find("foo");
      if (it != topicList.end())
      {
          //remove an ID from that topic:
          it->removeOne("bar");
      }
      
      1 Reply Last reply
      2
      • F Offline
        F Offline
        Ferni
        wrote on 31 May 2016, 12:39 last edited by
        #3

        Thanks for the reply! I realized that I don't need a MultiMap and I used a Map instead. I had an error when I used "auto" then I had to change a few things. Here is my code:

        if(topicList.contains("foo")){
            if(!topicList["foo"].contains("bar")){
                topicList["bar"].append("bar");
            }
        }else{
            QList<QString> aux;
            aux.append("bar");
            topicList.insert("foo",aux);
        }
        

        The first part checks if the topic "foo" exists. If it's true, then checks if the ID is already registered. I think that is faster to search only in the QList. Besides, the same ID could be subscribed to many topics.

        If there is no topic "foo", I have to add an element to my QMap<QString, QList<QString> >. It works (or at least I got no errors) but I'm not sure if there is another way to add my QList, a better way I mean.

        Again, many thanks for your reply, it helps me a lot :)

        Best regards,
        Fernando

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 31 May 2016, 17:15 last edited by Chris Kawa
          #4

          I had an error when I used "auto"

          auto is a c++11 keyword. If it's not enabled by default with your compiler you can add CONFIG += c++11 to your .pro file. If you don't want to use auto that's ok. Just more typing.

          I think that is faster

          This code is anything but fast. operator[] and contains() are search algorithms, quite heavy. Also remember that if operator[] doesn't find an item it creates it.
          Consider this code:

          if(topicList.contains("foo")){
              if(!topicList["foo"].contains("bar")){
                  topicList["bar"].append("bar");
              }
          }
          

          It does 4 searches and 1 insertion in the worst case. Now if written like this

          QMap<QString, QList<QString> >::iterator it = topicList.find("foo"); //or you could use auto if you enable c++11
          if (it != topicList.end())
          {
              QList<QString>::iterator it2 = it->find("bar");
              if (it2 != it->end())
                 it2->append("bar");
          }
          

          this is more code but does only 2 searches and 1 insertion in the worst case. This can be simplified like this:

          QList<QString>& foo = topicList["foo"]; //this will find a list or create an empty one if it wasn't there
          QList<QString>::iterator it = foo.find("bar");
          if (it != foo.end())
             foo.append("bar");
          

          As for the other part, the code is wrong. It will always overwrite entire list and you just want to add to an existing one or create one if it wasn't there, so instread of:

          QList<QString> aux;
          aux.append("bar");
          topicList.insert("foo",aux);
          

          it should be:

          topicList["foo"].append("bar");
          
          1 Reply Last reply
          2
          • F Offline
            F Offline
            Ferni
            wrote on 1 Jun 2016, 13:46 last edited by
            #5

            I'm such a newbie programmer :(
            I study electronics engineer and I like programming but I don't receive a good foundation in my university and I had to learn a lots of things by myself. Also, I haven't done any OOP for years and I'm new in Qt.
            Many many thanks!! I will pay more attention to my coding :)

            1 Reply Last reply
            0

            2/5

            30 May 2016, 19:58

            • Login

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