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. Resizing QList for Qt5
Qt 6.11 is out! See what's new in the release blog

Resizing QList for Qt5

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.2k Views 1 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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    Hi. I'm on Qt 5.15. I'm trying to create a QList of ints and fill them with 0s. I wanted to use resize() but it doesn't seem to be available for Qt5. Is there a workaround or do I have to manually append the list by quite a few times?

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

      @jsulm reserve does not resize the list :(

      @Dummie1138 You might want to use QVector instead, which has resize. In Qt6 QList and QVector are the same anyway.
      If not then you'll have to add them one by one e.g. like this, but it's not very efficient for a large n:

      list.reserve(n);
      std::fill_n(std::back_inserter(list), n, 0);
      
      1 Reply Last reply
      0
      • Dummie1138D Dummie1138

        Hi. I'm on Qt 5.15. I'm trying to create a QList of ints and fill them with 0s. I wanted to use resize() but it doesn't seem to be available for Qt5. Is there a workaround or do I have to manually append the list by quite a few times?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Dummie1138 https://doc.qt.io/qt-5/qlist.html#reserve

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Dummie1138D Offline
          Dummie1138D Offline
          Dummie1138
          wrote on last edited by
          #3

          Thanks a lot, I'm still new, wasn't aware that allocating the space would automatically resize the list.

          In regards to manually appending the list, I have the following code:

              for (int h = 0; h <= binMantissa.size(); h++){
                  binMantissa.replace(h, 0);
              }
          

          Is there a better way of doing it in Qt5 or is there a function that replaces all elements of a value?

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

            @jsulm reserve does not resize the list :(

            @Dummie1138 You might want to use QVector instead, which has resize. In Qt6 QList and QVector are the same anyway.
            If not then you'll have to add them one by one e.g. like this, but it's not very efficient for a large n:

            list.reserve(n);
            std::fill_n(std::back_inserter(list), n, 0);
            
            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