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?

How apply std::fill() to QList?

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 3.0k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on 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
    • EngelardE Engelard

      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.

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      EngelardE 1 Reply Last reply
      2
      • VRoninV VRonin

        @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

        EngelardE Offline
        EngelardE Offline
        Engelard
        wrote on 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
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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 ?

          EngelardE 1 Reply Last reply
          0
          • mrjjM mrjj

            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 ?

            EngelardE Offline
            EngelardE Offline
            Engelard
            wrote on 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> :)

            mrjjM 1 Reply Last reply
            0
            • EngelardE Engelard

              @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> :)

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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 ?
              EngelardE 1 Reply Last reply
              0
              • mrjjM mrjj

                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 ?
                EngelardE Offline
                EngelardE Offline
                Engelard
                wrote on 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 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

                  EngelardE 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

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

                    EngelardE Offline
                    EngelardE Offline
                    Engelard
                    wrote on 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 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
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 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

                        • Login

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