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. [solved] Model/view. Best way to add new items to Model and display them in View.
Forum Updated to NodeBB v4.3 + New Features

[solved] Model/view. Best way to add new items to Model and display them in View.

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.1k 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.
  • HarbH Offline
    HarbH Offline
    Harb
    wrote on last edited by Harb
    #1

    Hi guys!

    Continue to study model/view programming in Qt. I have implemented my own model, which is a subclass of QAbstractTableModel. I have my structure GraphData and QList<GraphData> which is container for my data. Also, as I said before I have implemented my own model, which is a wrapper QList<GraphData>(each row in model is my GraphData structure). My problem is that I don't understand yet, how to update Model and View properly, when I add new GraphData to the QList<GraphData>. The only idea which I have now, is to add empty row to the model through insertRows(row,count, index) implementation, then get QModelIndex for each column in this row and through setData(index,value,role) implementation change each value for each QModelIndex. But it seems quite ugly, there should be more convenient way to do that.

    1 Reply Last reply
    0
    • Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      As far as I understood the Model/View concept you can either implement inserRows() which will add a new "empty" or "default" item or you can do it manually outside of the model by wrapping the line of code that will append to your QList<GraphData> list in beginInsertRows() and endInsertRows().

      Note that you have to use beginInsertRows() and endInsertRows() in the insertRows() implementation as well. I think this is stated in the documentation.

      The purpose of beginInsertRows() is to let all views know that they should not read from the model while the purpose of endInsertRows() is to let the views know that it is time for an update. Hence you don't have to manually update your views at all.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        There's no need for outside of the model methods nor reimplementation of insertRows. You should add your custom type handling method to your model subclass e.g. insertGraphData(int rows, const GraphData& data) and do the begin/endInsertRows call in it.

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

        HarbH 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          There's no need for outside of the model methods nor reimplementation of insertRows. You should add your custom type handling method to your model subclass e.g. insertGraphData(int rows, const GraphData& data) and do the begin/endInsertRows call in it.

          HarbH Offline
          HarbH Offline
          Harb
          wrote on last edited by
          #4

          @SGaist Tnx, perfect!

          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