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 insert the whole array into the end of a QList?
Forum Updated to NodeBB v4.3 + New Features

How to insert the whole array into the end of a QList?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 2.2k Views 2 Watching
  • 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #2

    Hello!

    Have you check the append method - https://doc.qt.io/qt-5/qlist.html#append?
    It will add your value to the end of the QList.

    Code:

    QList<int> list;
    list.append(2);
    list.append(6);
    list.append(4);
    list.append(8);
    qDebug() << list;
    
    S 1 Reply Last reply
    0
    • Cobra91151C Cobra91151

      Hello!

      Have you check the append method - https://doc.qt.io/qt-5/qlist.html#append?
      It will add your value to the end of the QList.

      Code:

      QList<int> list;
      list.append(2);
      list.append(6);
      list.append(4);
      list.append(8);
      qDebug() << list;
      
      S Offline
      S Offline
      StudyQt1
      wrote on last edited by StudyQt1
      #3

      @Cobra91151 Thank you very much for your reply, can you make:

      list.append(2);
      list.append(6);
      list.append(4);
      list.append(8);
      

      into one line? Maybe sth like:

      int array[ 4 ] = { 2, 6, 4, 8 };
      QList<int> list;
      list.append(list.begin(), array, array+4);
      

      I am not sure if in QList there's such method

      1 Reply Last reply
      0
      • Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #4

        @StudyQt1

        You can use << (Left Shift) operator to add it on 1 line:

        QList<int> list;
        list << 2 << 6 << 4 << 8;
        qDebug() << list;
        

        Regarding usage of QVector in your case, please check out the @J-Hilk comment here:
        https://forum.qt.io/topic/107848/how-to-insert-different-arrays-into-a-qvector/8

        I think using QVector for such small data is not very appropriate but it's your choice. Happy coding!

        S 1 Reply Last reply
        0
        • Cobra91151C Cobra91151

          @StudyQt1

          You can use << (Left Shift) operator to add it on 1 line:

          QList<int> list;
          list << 2 << 6 << 4 << 8;
          qDebug() << list;
          

          Regarding usage of QVector in your case, please check out the @J-Hilk comment here:
          https://forum.qt.io/topic/107848/how-to-insert-different-arrays-into-a-qvector/8

          I think using QVector for such small data is not very appropriate but it's your choice. Happy coding!

          S Offline
          S Offline
          StudyQt1
          wrote on last edited by
          #5

          @Cobra91151 Thank you for your help, actually in my project int array[ 4 ] = { 2, 6, 4, 8 }; is sth like: int arr[10000] , so maybe list << 2 << 6 << 4 << 8; doesn't work for me. I am dealing with inserting large amount of data into QList

          Cobra91151C 1 Reply Last reply
          0
          • S StudyQt1

            @Cobra91151 Thank you for your help, actually in my project int array[ 4 ] = { 2, 6, 4, 8 }; is sth like: int arr[10000] , so maybe list << 2 << 6 << 4 << 8; doesn't work for me. I am dealing with inserting large amount of data into QList

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #6

            @StudyQt1

            So, in such case I would recommend to use QVector.

            Code:

            QVector<int> vArray = { 2, 6, 4, 8 };
            QVector<QVector<int>> vector;
            vector.push_back(vArray); // or vector << vArray;
            
            for (int i = 0; i < vector.size(); i++) {
                 qDebug() << vector.at(i);
            }
            

            Also, about inserting large amount of data, it is recommended to use loops.

            S 1 Reply Last reply
            0
            • Cobra91151C Cobra91151

              @StudyQt1

              So, in such case I would recommend to use QVector.

              Code:

              QVector<int> vArray = { 2, 6, 4, 8 };
              QVector<QVector<int>> vector;
              vector.push_back(vArray); // or vector << vArray;
              
              for (int i = 0; i < vector.size(); i++) {
                   qDebug() << vector.at(i);
              }
              

              Also, about inserting large amount of data, it is recommended to use loops.

              S Offline
              S Offline
              StudyQt1
              wrote on last edited by StudyQt1
              #7

              @Cobra91151 Sorry I didn't see your last comment. so are you saying sth like:

              int arr[10000];
              QList<int> list 
              for(int i = 0; i < 10000; i++)
              {
              list.append(arr[i]);
              }
              

              I guess this is what you mean by "Also, about inserting large amount of data, it is recommended to use loops."

              Cobra91151C 1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #8

                If you just want to construct a new QList out of an array then

                QList<int> values(std::begin(array), std::end(array));
                

                if you want to add values to existing list then

                values.reserve(values.size() + std::size(array));
                std::copy(std::begin(array), std::end(array), std::back_inserter(values));
                
                S Christian EhrlicherC 2 Replies Last reply
                3
                • Chris KawaC Chris Kawa

                  If you just want to construct a new QList out of an array then

                  QList<int> values(std::begin(array), std::end(array));
                  

                  if you want to add values to existing list then

                  values.reserve(values.size() + std::size(array));
                  std::copy(std::begin(array), std::end(array), std::back_inserter(values));
                  
                  S Offline
                  S Offline
                  StudyQt1
                  wrote on last edited by StudyQt1
                  #9

                  @Chris-Kawa Thank you very much for your reply, it seems Qlist is different from std::list

                  Chris KawaC 1 Reply Last reply
                  0
                  • S StudyQt1

                    @Cobra91151 Sorry I didn't see your last comment. so are you saying sth like:

                    int arr[10000];
                    QList<int> list 
                    for(int i = 0; i < 10000; i++)
                    {
                    list.append(arr[i]);
                    }
                    

                    I guess this is what you mean by "Also, about inserting large amount of data, it is recommended to use loops."

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by Cobra91151
                    #10

                    @StudyQt1

                    It is not recommended to mix standard array - int array[4] with QVector<int>.
                    Please check out this comment: https://forum.qt.io/topic/107848/how-to-insert-different-arrays-into-a-qvector/8
                    You can use std::array in the QVector. Please check out the examples below:

                    Code:

                    std::array<int, 4> array = { 2, 6, 4, 8 };
                    QVector<std::array<int, 4>> vector;
                    vector.push_back(array); // or vector << array;
                    
                    for (int i = 0; i < static_cast<int>(vector[0].size()); i++) {
                         qDebug() << vector[0].at(i);
                    }
                    

                    Adding values to QList by using loop:

                    int arr[4] = { 2, 6, 4, 8 };
                    int arrSize = sizeof(arr) / sizeof(*arr); // Gets the size of array
                    QList<int> list;
                    
                    for (int i = 0; i < arrSize; i++) {
                         list.append(arr[i]);
                    }
                    
                    qDebug() << list;
                    
                    S 1 Reply Last reply
                    0
                    • S StudyQt1

                      @Chris-Kawa Thank you very much for your reply, it seems Qlist is different from std::list

                      Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      it seems Qlist is different from std::list

                      Very different. in Qt6 QList and QVector are the same thing: Qt containers compared with std containers

                      1 Reply Last reply
                      0
                      • Cobra91151C Cobra91151

                        @StudyQt1

                        It is not recommended to mix standard array - int array[4] with QVector<int>.
                        Please check out this comment: https://forum.qt.io/topic/107848/how-to-insert-different-arrays-into-a-qvector/8
                        You can use std::array in the QVector. Please check out the examples below:

                        Code:

                        std::array<int, 4> array = { 2, 6, 4, 8 };
                        QVector<std::array<int, 4>> vector;
                        vector.push_back(array); // or vector << array;
                        
                        for (int i = 0; i < static_cast<int>(vector[0].size()); i++) {
                             qDebug() << vector[0].at(i);
                        }
                        

                        Adding values to QList by using loop:

                        int arr[4] = { 2, 6, 4, 8 };
                        int arrSize = sizeof(arr) / sizeof(*arr); // Gets the size of array
                        QList<int> list;
                        
                        for (int i = 0; i < arrSize; i++) {
                             list.append(arr[i]);
                        }
                        
                        qDebug() << list;
                        
                        S Offline
                        S Offline
                        StudyQt1
                        wrote on last edited by
                        #12

                        @Cobra91151 Thank you for your codes, let me test its speed with std::copy

                        1 Reply Last reply
                        0
                        • Chris KawaC Chris Kawa

                          If you just want to construct a new QList out of an array then

                          QList<int> values(std::begin(array), std::end(array));
                          

                          if you want to add values to existing list then

                          values.reserve(values.size() + std::size(array));
                          std::copy(std::begin(array), std::end(array), std::back_inserter(values));
                          
                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @Chris-Kawa said in How to insert the whole array into the end of a QList?:

                          If you just want to construct a new QList out of an array then
                          QList<int> values(std::begin(array), std::end(array));

                          if you want to add values to existing list then
                          values.reserve(values.size() + std::size(array));
                          std::copy(std::begin(array), std::end(array), std::back_inserter(values));

                          Or use the convenient QList::append(const QList<T> &o) :)

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          Chris KawaC 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            @Chris-Kawa said in How to insert the whole array into the end of a QList?:

                            If you just want to construct a new QList out of an array then
                            QList<int> values(std::begin(array), std::end(array));

                            if you want to add values to existing list then
                            values.reserve(values.size() + std::size(array));
                            std::copy(std::begin(array), std::end(array), std::back_inserter(values));

                            Or use the convenient QList::append(const QList<T> &o) :)

                            Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by Chris Kawa
                            #14

                            @Christian-Ehrlicher append is not good here. It takes a list, so you'd first have to construct it from the array and then append it, doing a copy of the values twice.

                            To be honest there's no good api in Qt to do this. My example does the copy once, but it copies the values one by one, which is wasteful for ints. Ideally you'd want to resize the list once and just memcpy the values. Unfortunately resize will initialize the new values, which is unnecessary, since we'd overwrite them anyway. Also can't do that in Qt5, since list might not be one chunk of memory.

                            It's a common problem actually, same in std. That's why there are proposals for something like resize_uninitialized(size) or resize(std::uninitialized, size), but nothing yet. Custom allocator can be used to circumvent that, but it's ugly as hell.

                            1 Reply Last reply
                            1
                            • Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #15

                              Ok, when you want to avoid this small allocation then your std::copy() is the correct way. But I doubt this is the real usecase here - I'm pretty sure in the real world there is already a second container.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              Chris KawaC 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                Ok, when you want to avoid this small allocation then your std::copy() is the correct way. But I doubt this is the real usecase here - I'm pretty sure in the real world there is already a second container.

                                Chris KawaC Offline
                                Chris KawaC Offline
                                Chris Kawa
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @Christian-Ehrlicher Sure, it depends a lot on the actual code and it might not be the case here, but I'm just pointing that out because I find this problem a lot in my particular line of work. There's some data in a non-Qt container from another lib and then I want to use it in some Qt api which takes a list. few hundred thousands of ints might not sound like a lot but it becomes a problem if you have a couple of sets like that and you need to update them e.g. 60 times a second while also doing a bunch of other work. Doing one 0.5Mb memcpy vs doing it twice one int at a time (and also updating the size counter by one each time) becomes a huge difference and a potential bottleneck. On some hardware you get a dedicated DMA channels for such cases and not using them would make this extra wasteful.

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

                                  As you guys are seeing, that's the problem with parallel container framerworks within the same language. I understand and appreciate the philosophy behind Qt containers (copy on write) but really with they would have just stuck with the STL stuff. :^P

                                  Chris KawaC 1 Reply Last reply
                                  0
                                  • Kent-DorfmanK Kent-Dorfman

                                    As you guys are seeing, that's the problem with parallel container framerworks within the same language. I understand and appreciate the philosophy behind Qt containers (copy on write) but really with they would have just stuck with the STL stuff. :^P

                                    Chris KawaC Offline
                                    Chris KawaC Offline
                                    Chris Kawa
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Kent-Dorfman Qt is older than STL and early STL implementations were shady to say the least. It's just legacy code now and, in this case at least, STL has the same problem.
                                    Other languages often compromise performance for ease of use and don't give you any say in it. The nice thing about C++ is also the ugly thing about it - if defaults don't meet your needs you can roll your own, and so many do. My company for example has its own standard library, which always puts low level performance above everything else, but has some non-trivial gotchas as a result. Qt usually follows the "easy to use and good enough for average case" mantra, but sometimes interacts poorly with non-Qt code.

                                    1 Reply Last reply
                                    2

                                    • Login

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