Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

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

    General and Desktop
    4
    5
    413
    Loading More Posts
    • 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
      Asperamanca last edited by

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

      VRonin J.Hilk Gojir4 3 Replies Last reply Reply Quote 0
      • VRonin
        VRonin @Asperamanca last edited by VRonin

        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 Reply Quote 5
        • J.Hilk
          J.Hilk Moderators @Asperamanca last edited by

          @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

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

          1 Reply Last reply Reply Quote 3
          • Gojir4
            Gojir4 @Asperamanca last edited by

            @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 Reply Quote 2
            • A
              Asperamanca last edited by Asperamanca

              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 Reply Quote 0
              • First post
                Last post