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 apply std::fill() to QList?
QtWS25 Last Chance

How apply std::fill() to QList?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 2.6k 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.
  • E Offline
    E Offline
    Engelard
    wrote on 19 Nov 2018, 15:44 last edited by Engelard
    #1

    Hi, i want to fill my QList<QStandardItem> with initial objects, all of them of course the same, but since QList don't have .resize function(i read google, still can't answer why Vector have when List have'nt) i can't set size of my list so i sould able to do desired thing:

    std::fill(stdList, new QStandardItem("initial val"));
    

    Do that manually through the loop take alot of time.

    V 1 Reply Last reply 19 Nov 2018, 17:15
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 19 Nov 2018, 16:15 last edited by mrjj
      #2

      Hi
      it has
      http://doc.qt.io/qt-5/qlist.html#reserve

      Also you show code as

      QList<QStandardItem>
      
      not as
      QList<QStandardItem *>
      

      so i wonder what you try ?

      It would not be a good idea to preallocate a list with
      QList<QStandardItem> ( none pointers) and give them to a view using &.

      1 Reply Last reply
      3
      • E Engelard
        19 Nov 2018, 15:44

        Hi, i want to fill my QList<QStandardItem> with initial objects, all of them of course the same, but since QList don't have .resize function(i read google, still can't answer why Vector have when List have'nt) i can't set size of my list so i sould able to do desired thing:

        std::fill(stdList, new QStandardItem("initial val"));
        

        Do that manually through the loop take alot of time.

        V Offline
        V Offline
        VRonin
        wrote on 19 Nov 2018, 17:15 last edited by VRonin
        #3

        @Engelard said in How apply std::fill() to QList?:

        all of them of course the same

        Define "the same".
        Exactly the same object occupying the same place in memory or distinct objects with identical properties?.

        Why would you need a list of QStandardItem?! QStandardItemModel is already a list of QStandardItem, no need to duplicate it.

        @mrjj said in How apply std::fill() to QList?:

        It would not be a good idea to preallocate a list with
        QList<QStandardItem> ( none pointers) and give them to a view using &.

        QStandardItemModel takes ownership of the items so I think the suggestion might lead to unexpected tragedies

        "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

        E 1 Reply Last reply 19 Nov 2018, 17:34
        2
        • V VRonin
          19 Nov 2018, 17:15

          @Engelard said in How apply std::fill() to QList?:

          all of them of course the same

          Define "the same".
          Exactly the same object occupying the same place in memory or distinct objects with identical properties?.

          Why would you need a list of QStandardItem?! QStandardItemModel is already a list of QStandardItem, no need to duplicate it.

          @mrjj said in How apply std::fill() to QList?:

          It would not be a good idea to preallocate a list with
          QList<QStandardItem> ( none pointers) and give them to a view using &.

          QStandardItemModel takes ownership of the items so I think the suggestion might lead to unexpected tragedies

          E Offline
          E Offline
          Engelard
          wrote on 19 Nov 2018, 17:34 last edited by Engelard
          #4

          @VRonin said in How apply std::fill() to QList?:

          or distinct objects with identical properties?.

          that one.

          @VRonin said in How apply std::fill() to QList?:

          Why would you need a list of QStandardItem?! QStandardItemModel is already a list of QStandardItem, no need to duplicate it.

          I should fill that model with the data first, then this data(list of QStandardItems) add to my model?

          @mrjj tnx for reserve function.

          Seems qFill took it like that

          values.reserve(predefinedSize);
          qFill(values, new QStandardItem("string"));

          But any suggestion for doing such stuff better? I don't feel confident about all this..

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 19 Nov 2018, 17:54 last edited by mrjj
            #5

            Hi
            @VRonin

            • so I think the suggestion might lead to unexpected tragedies
              You seems to have missed the "not good idea part" ? :)
              or i misread your line?
              But yes, you are right, should have written "hidden disaster to happen."

            @Engelard
            As VRonin says, the model is already such list so not sure why you want another
            QList with items ?

            E 1 Reply Last reply 19 Nov 2018, 18:57
            0
            • M mrjj
              19 Nov 2018, 17:54

              Hi
              @VRonin

              • so I think the suggestion might lead to unexpected tragedies
                You seems to have missed the "not good idea part" ? :)
                or i misread your line?
                But yes, you are right, should have written "hidden disaster to happen."

              @Engelard
              As VRonin says, the model is already such list so not sure why you want another
              QList with items ?

              E Offline
              E Offline
              Engelard
              wrote on 19 Nov 2018, 18:57 last edited by Engelard
              #6

              @mrjj said in How apply std::fill() to QList?:

              As VRonin says, the model is already such list so not sure why you want another
              QList with items ?

              Well, starting with stringListModel where you should fill it with a data - QStringList.
              So i assumed that for QStandardItemModel you should fill it with data - QStandardItem.

              QStandardItemModel->appendColumn() takes only QList<QStandardItem> nothing else, so i don't understand why you are even asking me about why am i trying to make QList<QStandardItem> :)

              M 1 Reply Last reply 19 Nov 2018, 19:20
              0
              • E Engelard
                19 Nov 2018, 18:57

                @mrjj said in How apply std::fill() to QList?:

                As VRonin says, the model is already such list so not sure why you want another
                QList with items ?

                Well, starting with stringListModel where you should fill it with a data - QStringList.
                So i assumed that for QStandardItemModel you should fill it with data - QStandardItem.

                QStandardItemModel->appendColumn() takes only QList<QStandardItem> nothing else, so i don't understand why you are even asking me about why am i trying to make QList<QStandardItem> :)

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 19 Nov 2018, 19:20 last edited by
                #7

                Hi
                I think the non pointer version fooled me :)
                Now it all make sense.
                However, normally i use
                appendRow(const QList<QStandardItem *> &items)
                to insert a row of data.
                like```
                QList<QStandardItem *> items;

                items.append(new QStandardItem("Column 1 Text"));
                items.append(new QStandardItem("Column 2 Text"));

                QStandardItemModel* model = new QStandardItemModel(this);

                model->setColumnCount(2);
                model->appendRow(items);

                
                but appendColumn is fine too of cause depending on your data layout. 
                
                Those 100.000 rows. how many columns do they have ?
                E 1 Reply Last reply 19 Nov 2018, 20:05
                0
                • M mrjj
                  19 Nov 2018, 19:20

                  Hi
                  I think the non pointer version fooled me :)
                  Now it all make sense.
                  However, normally i use
                  appendRow(const QList<QStandardItem *> &items)
                  to insert a row of data.
                  like```
                  QList<QStandardItem *> items;

                  items.append(new QStandardItem("Column 1 Text"));
                  items.append(new QStandardItem("Column 2 Text"));

                  QStandardItemModel* model = new QStandardItemModel(this);

                  model->setColumnCount(2);
                  model->appendRow(items);

                  
                  but appendColumn is fine too of cause depending on your data layout. 
                  
                  Those 100.000 rows. how many columns do they have ?
                  E Offline
                  E Offline
                  Engelard
                  wrote on 19 Nov 2018, 20:05 last edited by
                  #8

                  @mrjj said in How apply std::fill() to QList?:

                  Those 100.000 rows. how many columns do they have ?

                  Only two, only column approach is reasonable, since first column will append only once and will become unchangeable till the very end. And second column initially should be with the same value.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 19 Nov 2018, 20:08 last edited by
                    #9

                    @Engelard said in How apply std::fill() to QList?:

                    Those 100.000 rows. how many columns do they have ?

                    This definitely shows that choosing a QStandardItemModel here was the wrong idea.

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

                    E 1 Reply Last reply 19 Nov 2018, 20:13
                    0
                    • Christian EhrlicherC Christian Ehrlicher
                      19 Nov 2018, 20:08

                      @Engelard said in How apply std::fill() to QList?:

                      Those 100.000 rows. how many columns do they have ?

                      This definitely shows that choosing a QStandardItemModel here was the wrong idea.

                      E Offline
                      E Offline
                      Engelard
                      wrote on 19 Nov 2018, 20:13 last edited by
                      #10

                      @Christian-Ehrlicher There is no other options for QTableView, it tooks only that kind of types.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 19 Nov 2018, 20:38 last edited by
                        #11

                        Sure there is: QAbstractTableModel

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3
                        • M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 19 Nov 2018, 20:44 last edited by
                          #12

                          Hi
                          This sample might be of interest.
                          http://doc.qt.io/qt-5/qtwidgets-itemviews-addressbook-example.html
                          It both uses QAbstractTableModel and a QSortFilterProxyModel to filter it.

                          1 Reply Last reply
                          3

                          1/12

                          19 Nov 2018, 15:44

                          • Login

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