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 efficiently create a QList with a single item in one statement?

How to efficiently create a QList with a single item in one statement?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.3k 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    How do I create a QList containing a single item in a way I can use as input value for a method, e.g.

    QHash<int,QList<int>> listHash;
    listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
    

    With std::vector it's easy:

    QHash<int,std::vector<int>> vectorHash;
    listHash.insert(0,std::vector<int>(1,5));  // Create vector with 1 item of value 5
    

    However, I need to stick to an existing interface, so I have to use QList in my case...

    VRoninV J.HilkJ Gojir4G 3 Replies Last reply
    0
    • A Asperamanca

      How do I create a QList containing a single item in a way I can use as input value for a method, e.g.

      QHash<int,QList<int>> listHash;
      listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
      

      With std::vector it's easy:

      QHash<int,std::vector<int>> vectorHash;
      listHash.insert(0,std::vector<int>(1,5));  // Create vector with 1 item of value 5
      

      However, I need to stick to an existing interface, so I have to use QList in my case...

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

      listHash.insert(0,QList<int>{5});

      listHash.insert(0,QList<int>{5,4,3,2,1});

      (if it doesn't work double the curly brakets)

      "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
      5
      • A Asperamanca

        How do I create a QList containing a single item in a way I can use as input value for a method, e.g.

        QHash<int,QList<int>> listHash;
        listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
        

        With std::vector it's easy:

        QHash<int,std::vector<int>> vectorHash;
        listHash.insert(0,std::vector<int>(1,5));  // Create vector with 1 item of value 5
        

        However, I need to stick to an existing interface, so I have to use QList in my case...

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @asperamanca QList only offers the initializer list, QVector on the other hand, offers, like std::vector , a QVector(int size, const T &value) overload.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • A Asperamanca

          How do I create a QList containing a single item in a way I can use as input value for a method, e.g.

          QHash<int,QList<int>> listHash;
          listHash.insert(0,QList<int>(???)); //What kind of constructor or static creation method can I use here?
          

          With std::vector it's easy:

          QHash<int,std::vector<int>> vectorHash;
          listHash.insert(0,std::vector<int>(1,5));  // Create vector with 1 item of value 5
          

          However, I need to stick to an existing interface, so I have to use QList in my case...

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @asperamanca From c++11 you can initialize lists, vectors, maps, hashes, etc... using curly brackets:

              QHash<int,QList<int>> listHash;
              listHash.insert(0, {1});
              listHash.insert(1, QList<int>{1});
              listHash.insert(2, {1, 2, 3, 4});
              
              QHash<int,QList<int>> listHash2 = {
                  {0, {1, 2, 3}},
                  {1, {4, 5}},
                  {2, {6, 7, 8}},
              };
          
          1 Reply Last reply
          2
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by Asperamanca
            #5

            OK, I simplified the problem too much. I do not have a constant value for initialization, but an argument:

            void addToHash(const int key, const int value)
            {
               if (m_Hash.contains(key))
               {
                  m_Hash[key].append(value);
               }
               else
               {
                  m_Hash.insert(key,QList<int>{value});
               }
            }
            

            Here, the initializer list causes a compiler error:
            'initializing': cannot convert from 'initializer list' to 'QList<int>'

            EDIT:
            And here I circumvent my original problem:

            void addToHash(const int key, const int value)
            {
               m_Hash[key].append(value);
            }
            

            ...since operator[] default-constructs an entry if none exists.

            1 Reply Last reply
            0

            • Login

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