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. QStandardItemModel appendRow doesn't update QML TreeView
Forum Updated to NodeBB v4.3 + New Features

QStandardItemModel appendRow doesn't update QML TreeView

Scheduled Pinned Locked Moved Solved General and Desktop
qt quickmodeltreeview
24 Posts 3 Posters 8.6k 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.
  • B Offline
    B Offline
    BitteWenden
    wrote on 23 Apr 2016, 22:49 last edited by
    #6

    Nothing apart from custom roles I use in childEntry->setData.

    class ProjectTreeModel : public QStandardItemModel
    {
        Q_OBJECT
    public:
        explicit ProjectTreeModel(QObject *parent = 0);
        virtual ~ProjectTreeModel() = default;
    
        Q_INVOKABLE QString initializeModel(QString dirLoation);
        enum ProjectTreeModel_Roles
        {
            ProjectTreeModel_Role_Name = Qt::DisplayRole,
            ProjectTreeModel_Role_Type = Qt::WhatsThisRole,
            ProjectTreeModel_Role_Icon = Qt::DecorationRole
        };
    
        QHash<int, QByteArray> roleNames() const override;
    
    private:
        void addChildEntry( const QString& name, const QString& type, const QString& icon, QStandardItem parent);
        void addRootEntry( const QString& name, const QString& type, const QString& icon);
        QHash<int, QByteArray> m_roleNameMapping;
    };
    

    This is my class, so as you can see there is nothing really done with it.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Apr 2016, 22:54 last edited by
      #7

      How/where are you appending rows outside the constructor ?

      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
      0
      • B Offline
        B Offline
        BitteWenden
        wrote on 23 Apr 2016, 22:57 last edited by
        #8

        In the addRootEntry (and later in the addChildEntry where it should add a child to a given QStandardItem) in reaction to a button press.

        auto rootEntry = new QStandardItem( name );
        
            rootEntry->setData( icon, ProjectTreeModel_Role_Icon );
            rootEntry->setData( type, ProjectTreeModel_Role_Type );
            rootEntry->setData( name, ProjectTreeModel_Role_Name );
        
            invisibleRootItem()->appendRow(rootEntry);
        

        This is what it does.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BitteWenden
          wrote on 24 Apr 2016, 16:19 last edited by
          #9

          Do I need to tell my view somehow that the model got updated?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 24 Apr 2016, 20:07 last edited by
            #10

            I'd emit dataChanged with the new index passing your custom roles.

            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
            0
            • B Offline
              B Offline
              BitteWenden
              wrote on 24 Apr 2016, 20:26 last edited by
              #11

              I added emit dataChanged(QModelIndex(), QModelIndex(), roles); after my appendRow, which should update the whole view(?), but it still doesn't work.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BitteWenden
                wrote on 24 Apr 2016, 20:31 last edited by
                #12

                This is my whole class right now, in case that helps:

                ProjectTreeModel::ProjectTreeModel(QObject *parent) :
                    QStandardItemModel(parent)
                {
                
                    m_roleNameMapping[ProjectTreeModel_Role_Name] = "name_role";
                    m_roleNameMapping[ProjectTreeModel_Role_Type] = "type_role";
                    m_roleNameMapping[ProjectTreeModel_Role_Icon] = "icon_role";
                
                
                }
                
                void ProjectTreeModel::addRootEntry( const QString& name, const QString& type, const QString& icon)
                {
                    auto rootEntry = new QStandardItem( name );
                
                    rootEntry->setData( icon, ProjectTreeModel_Role_Icon );
                    rootEntry->setData( type, ProjectTreeModel_Role_Type );
                    rootEntry->setData( name, ProjectTreeModel_Role_Name );
                
                    invisibleRootItem()->appendRow(rootEntry);
                    qDebug() << rootEntry;
                    qDebug() << this->rowCount();
                
                    QVector<int> vector(0);
                    vector.append(ProjectTreeModel_Role_Icon);
                    vector.append(ProjectTreeModel_Role_Type);
                    vector.append(ProjectTreeModel_Role_Name);
                
                    emit dataChanged(QModelIndex(), QModelIndex(), vector);
                }
                
                
                QHash<int, QByteArray> ProjectTreeModel::roleNames() const
                {
                    return m_roleNameMapping;
                }
                
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 24 Apr 2016, 20:41 last edited by
                  #13

                  AFAIK, no. You are passing two invalid indexes. If you want to trigger a refresh, give the top left and bottom right indexes of the part of the model that should get refreshed on the view.

                  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
                  0
                  • B Offline
                    B Offline
                    BitteWenden
                    wrote on 25 Apr 2016, 06:56 last edited by
                    #14

                    Okay, tried it with emit dataChanged(invisibleRootItem()->index(), rootEntry->index(), vector); which should work, but it doesn't.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BitteWenden
                      wrote on 25 Apr 2016, 17:37 last edited by
                      #15

                      Or at least I think that should update everything from the root item to the newly created entry.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 25 Apr 2016, 20:48 last edited by
                        #16

                        How are you adding that new row outside the constructor ?

                        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
                        0
                        • B Offline
                          B Offline
                          BitteWenden
                          wrote on 25 Apr 2016, 21:20 last edited by
                          #17

                          With the appendRow() method (on the invisible root item) as mentioned earlier.

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 25 Apr 2016, 22:16 last edited by
                            #18

                            I meant, where are you calling appendRow ?

                            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
                            0
                            • B Offline
                              B Offline
                              BitteWenden
                              wrote on 26 Apr 2016, 04:52 last edited by
                              #19

                              In a function that is called in response to a button click. (that function calls addRootItem multiple times)

                              1 Reply Last reply
                              0
                              • O Offline
                                O Offline
                                Orkblutt
                                wrote on 26 Apr 2016, 07:50 last edited by Orkblutt
                                #20

                                What about using beginInsertRows() / endInsertRow() ?

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  BitteWenden
                                  wrote on 26 Apr 2016, 13:39 last edited by
                                  #21

                                  That doesn't seem to work either.

                                  1 Reply Last reply
                                  0
                                  • B Offline
                                    B Offline
                                    BitteWenden
                                    wrote on 26 Apr 2016, 19:54 last edited by
                                    #22

                                    Does anyone know of some piece of sample code for a dynamic model with Qt Quick and QStandardItemModel where you can add rows and edit items after the construction?

                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      BitteWenden
                                      wrote on 27 Apr 2016, 15:17 last edited by
                                      #23

                                      Oh, Damnit. I had a ProjectTreeViewModel { id:projecttreeviewmodel } in my UI components and used that instead of my treeviewmodel added as a context property.

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 27 Apr 2016, 21:44 last edited by
                                        #24

                                        Sneaky one !

                                        Glad you found out :)

                                        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
                                        0

                                        15/24

                                        25 Apr 2016, 17:37

                                        • Login

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