Important: Please read the Qt Code of Conduct -

QTreeView setRootIndex & changing header - incorrect columnCount...?

  • Hey

    So I have a bit of a "sigh" moment, I'm trying to implement setRootIndex & change header data at the same time.

    The changing of header "text" seems to work, however, the count of items seem to not work. I ensure that the columnCount on old rootIndex and new rootIndex return proper column count, and I can see it in print as well during columnCount() call, but the number of columns doesn't change. What am I missing here?


  • Moderators

    Possibly calls to beginInsertColumns or beginRemoveColumns are missing.

    setRootIndex() will cause a model reset, but if you change header after that, the view won't know that column count has changed.

  • @sierdzio hmm yeah which is the problem. I change header before changing rootIndex. I mean, the header is taken dynamically from root index, right?

    I mean: = header text items.
    So if I change =
    it should be a new header? I mean I know it works because it returns the correct number of columnCount & header display new text, but header doesn't add/remove columns... depending on data.size() which is what it being used to determine column draw? I must be missing something.. sigh.

  • Moderators

    Number of columns is a property of the model, not the header. So your model needs to return correct columnCount(). And if the count changes, model has to notify the views (by begin/end methods, resetModel or dataChanged).

  • Lifetime Qt Champion

    Using setRootIndex() only works reliable when the header count does not change and imo is not meant for anything else, see also
    I would be happy to get this stuff removed since it's just causing trouble for all use cases where more than the root index should change - all this can be (re)implemented with a second model or a QSortFilterProxyModel or similar.

  • I'm using abstractItemModel / my own item, I ensure that columnCount return correct number of columns, yet it's still wrong.

    In regard to using proxyModels, I'd love to use it, but I only found few examples and I'm struggling as to how I would do it. Any hints wold be great, I would prefer to use a proxy instead here.

    I literally have no idea how I can simulate setRootIndex with proxyModel, since I need to see parentItem of the item that I wish to set as rootitem and I don't want to see these parent items... here was me early attempt >

    @Christian-Ehrlicher I'm totally blaming you for me going this setRootIndex road as I was trying proxy early and you said to use this : p

Log in to reply