QtQuick Treeview + PySide6
-
Good Day
Im a .Net developer, C# for the past 2 years who decided i want to learn python and the Qt framework. So i would say i have somewhat of an okay skill level.
Im litterally at the point of giving up on python and Qt. Documentation is extremely confusing, the examples i find is only in c++ which i hardly have much knowledge of.
ALL I WANT TO DO IS CREATE A TREEVIEW 😠with a couple columns but it has been a week and i have been able to create some basic treeviews that hardly works.
I have tried to follow this link but i would like to seperate the UI from my python and qml.
I am at the point of just resorting to going back to my c# environment of developing this app as i am not doing this professionally but as a hobby.
What i am aiming for is a treeview that looks like this but with my own data link text
Any links to guides or more explanatory info would be greatly appreciated
-
I have no experience with Python and Qt but I just wanted to check something. You have posted in the QML area and you mention QML in your post, but the link you mentioned is using Qt Widgets. Qt supports two different approaches to GUIs: Widgets and QML. Generally speaking, you choose one and stick with it. (At a more advanced level, it is possible to embed QML in a Widgets app but not vice versa.)
In principle, QML does provide the good UI separation that you want.
Unfortunately
TreeView
is probably one of the hardest components to get going with because, unlikeListView
for example, there are no simple built-in models that you can use to get started with. You pretty much have to implement aQAbstractItemModel
from the outset. It is further complicated by most documentation being geared towards C++, and also by theTreeView
in Qt 6 not being the same as theTreeView
in previous releases, which means you have to be careful if using older information.I would break it down as follows:
- Maybe start with a simpler view like a
ListView
populated from aListModel
purely in QML. - Then learn how to replace the
ListModel
with a model provided from your Python backend. This will be an implementation of aQAbstractListModel
. - Once you are comfortable with how this works, look at implementing a
QAbstractItemModel
to support aTreeView
. AlthoughTreeView
itself has changed, the way you implement the backend model should be stable so you don't need to worry about looking at older guides for this. - If you can successfully implement the model, the QML for the
TreeView
should be relatively straightforward. For guidance on the QML side, you should be able to adapt examples based on a C++ backend as QML is agnostic as to whether this is C++ or Python.
- Maybe start with a simpler view like a
-
I found this example of implementing a tree model in Python. I believe it is an adaptation of one of the standard examples. Note that this is targeting Qt Widgets but the same model could be used to drive a QML
TreeView
. -
I have no experience with Python and Qt but I just wanted to check something. You have posted in the QML area and you mention QML in your post, but the link you mentioned is using Qt Widgets. Qt supports two different approaches to GUIs: Widgets and QML. Generally speaking, you choose one and stick with it. (At a more advanced level, it is possible to embed QML in a Widgets app but not vice versa.)
In principle, QML does provide the good UI separation that you want.
Unfortunately
TreeView
is probably one of the hardest components to get going with because, unlikeListView
for example, there are no simple built-in models that you can use to get started with. You pretty much have to implement aQAbstractItemModel
from the outset. It is further complicated by most documentation being geared towards C++, and also by theTreeView
in Qt 6 not being the same as theTreeView
in previous releases, which means you have to be careful if using older information.I would break it down as follows:
- Maybe start with a simpler view like a
ListView
populated from aListModel
purely in QML. - Then learn how to replace the
ListModel
with a model provided from your Python backend. This will be an implementation of aQAbstractListModel
. - Once you are comfortable with how this works, look at implementing a
QAbstractItemModel
to support aTreeView
. AlthoughTreeView
itself has changed, the way you implement the backend model should be stable so you don't need to worry about looking at older guides for this. - If you can successfully implement the model, the QML for the
TreeView
should be relatively straightforward. For guidance on the QML side, you should be able to adapt examples based on a C++ backend as QML is agnostic as to whether this is C++ or Python.
Thank you very much for your informative explanation.
Now that you mention that the link refers to widgets, i clearly see it. The documentation is a little confusing sometimes and not super clear if you are not very familiar with the Qt framework i think.
I will definitely make use of your advice in using the ListView model and the link you have provided.
Thank you very much
- Maybe start with a simpler view like a
-
-
Thank you very much for your informative explanation.
Now that you mention that the link refers to widgets, i clearly see it. The documentation is a little confusing sometimes and not super clear if you are not very familiar with the Qt framework i think.
I will definitely make use of your advice in using the ListView model and the link you have provided.
Thank you very much
@RynoJ
Two observations:-
C# is a lot closer to C++ than to Python, which is a completely different language.
-
QML is a declarative language, and not really a programming language. Quite different from normal procedural programming like C# (or for that matter C++ or Python). It is a totally different paradigm. Depending on what you did in C# about e.g. the UI, using Qt widgets (with either C++ or Python) would likely be a lot closer than anything QML.
-