QML TreeView using only QtQuick 2 works fine?

  • Hi.
    Quite a few people, including myself, have been regretting and wondering why TreeView is only available in Controls 1 which are deprecated since Qt 5.12.

    As it turns out however, it seems quite straight-forward to adapt the TreeView code to depend exclusively on Controls 2 elements.

    For instance, I just copied and merged all the QML code for a TreeView into a single .qml file that only depends on the following imports:

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQml.Models 2.15

    A working TreeView then only requires a local copy of the original TableViewColumn.qml file (which itself only depends on the QtQuick 2.15 module), and a local copy of the QQuickTreeModelAdaptor class source (which simply implements a QAbstractListModel)

    Internally, the original TreeView uses ScrollView, ListView and FocusScope, all of which have their equivalents in QtQuick2 (the only property I needed to comment out was the frameVisible property, which does not exist in ScrollView 2, and get rid of the viewport property).

    Aside from the styling parts which I either eliminated or hard-coded,
    and the scrollbar code which I currently disabled, it seems to work just fine: the tree displays, expands/collapses and behaves as expected, so far.

    I wanted to upload the code for that self-contained TreeView.qml file here, but unfortunately, posts are not allowed to be that long.

  • That sounds great. Can you share it via Github?

  • @mnesarco said in QML TreeView using only QtQuick 2 just works fine?:

    Can you share it via Github?

    I uploaded the filesystembrowser example using the modified TreeView here:

  • A questions: Is it possible to make a copy of QQuickTreeModelAdaptor in a LGPL licensed program or would this violate the LGPL?

  • Lifetime Qt Champion

    @maxwell31 What exactly do you mean? Do you want to modify QQuickTreeModelAdaptor? Or why else you would need to "copy"?
    If you want to modify QQuickTreeModelAdaptor then you have ot make your changes open source (LGPL requirement).

Log in to reply