Qt World Summit: Submit your Presentation

Qt Model/View inserting data.

  • Hi,
    I'm implementing Qt Model/View for tree model. I've TreeModel derived from QAbstractItemModel and have TreeItem for underlying model.
    My question is how to insert data to my model. Should I use 1) TreeModel::insertRows with (beginInsertRows/endInsertRows) or 2) insert data directly to TreeItem.
    If I use 2) how to notify view about insertion, and if I use 1) how to insert data in bulk , will every beginInsertRows/endInsertRows calls hit performance?

    Thank you.

    Edit: TreeModel::insertRow -> TreeModel::insertRows

  • Use 1) but instead of reimplementing TreeModel::insertRow reimplement TreeModel::insertRows so you can add data in bulk

  • Moderators

    to add up to @VRonin:
    insertRow implicitly calls insertRows with the according parameters.
    As a general principle, only virtual methods are meant to be overridden.

  • @VRonin
    Thank you for replay. What about if I have to add data in bulk, but its not always jost rows, but rows with children. The thing is I cant
    just create root node for row with children, and add it, becouse I'm creating node hierarchy (file/folder structure) on the fly, so if I already have node for folder, I'm just adding child node, otherwise I must create folder node.

    I suppose there is no straight way, but must do optimisation for myself? So 1) is prefered way.

  • If you use 1) and the various calls to beginInsertRows/endInsertRows are done in sequence without the control going back to the event loop, the view should optimise the repaint automatically

  • Moderators

    @BranislavM said in Qt Model/View inserting data.:

    Thank you for replay. What about if I have to add data in bulk, but its not always jost rows, but rows with children.

    then (2) is probably better.

    void addDataWithChildren()
        // add data to your internal data structure with children, the view then will ask for the childCount of the inserted row when it needs to

  • I started with (2) approach as @raven-worx supposed, and it's good for initialization. But I wanted to use the same code between beginInsertRows/endInsertRows calls for adding files, and problem is sometimes nodes already exist, and sometimes I need to add missing nodes, to make path structure. So its not always the same parent node where rows are inserted.
    I suppose I need to use combination of (1) and (2).

Log in to reply