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. Notify QTableView to update data
Forum Updated to NodeBB v4.3 + New Features

Notify QTableView to update data

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 432 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.
  • P Offline
    P Offline
    Pavel Romankov
    wrote on last edited by
    #1

    Hello.
    I have data source with its own interface. It hase methods like add(QString &name), remove(QString &name) and others.
    Also I have model which is inherited from QAbstractTableModel, it implements all required methods. Model connects to data source and receive data via its interface.
    And I have QTableView which is connected to my model.
    After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?

    JonBJ 1 Reply Last reply
    0
    • P Pavel Romankov

      Hello.
      I have data source with its own interface. It hase methods like add(QString &name), remove(QString &name) and others.
      Also I have model which is inherited from QAbstractTableModel, it implements all required methods. Model connects to data source and receive data via its interface.
      And I have QTableView which is connected to my model.
      After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Pavel-Romankov said in Notify QTableView to update data:

      Also I have model which is inherited from QAbstractTableModel, it implements all required methods.
      After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?

      Have you read https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing, which tells you what you need to implement in your subclass? For example, add()ing will invoke insertRows(). When your implementation correctly calls begin/endInsertRows() any attached views will update correctly; if you fail to implement this correctly they will not.

      P 1 Reply Last reply
      2
      • JonBJ JonB

        @Pavel-Romankov said in Notify QTableView to update data:

        Also I have model which is inherited from QAbstractTableModel, it implements all required methods.
        After I add item with add() method to data source, what is the right way to notify model that data was changed and update TableView?

        Have you read https://doc.qt.io/qt-6/qabstracttablemodel.html#subclassing, which tells you what you need to implement in your subclass? For example, add()ing will invoke insertRows(). When your implementation correctly calls begin/endInsertRows() any attached views will update correctly; if you fail to implement this correctly they will not.

        P Offline
        P Offline
        Pavel Romankov
        wrote on last edited by
        #3

        @JonB

        void beginInsertRows(const QModelIndex &parent, int first, int last);
        

        requires first and last parameters. But in my case I dont know them. When I add item to data source by

        dataSource.add("new item")
        

        it doesnt return number of row.

        JonBJ 1 Reply Last reply
        0
        • P Pavel Romankov

          @JonB

          void beginInsertRows(const QModelIndex &parent, int first, int last);
          

          requires first and last parameters. But in my case I dont know them. When I add item to data source by

          dataSource.add("new item")
          

          it doesnt return number of row.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Pavel-Romankov said in Notify QTableView to update data:

          But in my case I dont know them

          You must know them, since it is your own model! You know where you add the row(s), you need to issue the correct corresponding call.

          P 1 Reply Last reply
          0
          • JonBJ JonB

            @Pavel-Romankov said in Notify QTableView to update data:

            But in my case I dont know them

            You must know them, since it is your own model! You know where you add the row(s), you need to issue the correct corresponding call.

            P Offline
            P Offline
            Pavel Romankov
            wrote on last edited by
            #5

            @JonB
            So i need:

            • add new item with
            dataSorce.add("item");
            
            • get row number of new item.
            • notify model about this. In dataSource
            emit rowInserted(int rowNumber);
            
            • in model call
            beginInsertRows(QModelIndex(), rowNumber, rowNumber + 1);
            endInsertRows();
            

            right?

            JonBJ 1 Reply Last reply
            0
            • P Pavel Romankov

              @JonB
              So i need:

              • add new item with
              dataSorce.add("item");
              
              • get row number of new item.
              • notify model about this. In dataSource
              emit rowInserted(int rowNumber);
              
              • in model call
              beginInsertRows(QModelIndex(), rowNumber, rowNumber + 1);
              endInsertRows();
              

              right?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Pavel-Romankov
              You should put your adding into your model call (dataSorce.add("item")?) between the begin/endInsertRows().

              I believe for inserting one row your call should be

              beginInsertRows(QModelIndex(), rowNumber, rowNumber);
              

              not rowNumber + 1 for last, that should be the row number it will end up as:

              first and last are the row numbers that the new rows will have after they have been inserted.

              I am drawing a mental blank on emit rowInserted(int rowNumber);, I don't think you do anything for that, doesn't QAbstractTableModel do that for you from endInsertRows()? TBH I'm not sure, see https://forum.qt.io/topic/66484/how-to-update-qtableview-when-rows-are-inserted-to-it. Maybe see whether that signal gets emitted for you or whether you have to do it....

              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