QML ListView delegate provider component - qt

Currently im working on a complex program that parse large nested json into a tree and display it. I created a tree to parse data and a model to query data from tree to display it in QML ListView.
Now i need to create a some kind of delegate provider for this model. Each element in tree have its own delegate with different set of options and different qml items. And there must be an option to load new type of delegates in the runtime.
Is there are nice solution to do that? Some kind of custom C++ Loader component? I need an advise. I already know that it can be done using switch case and qml loader. But this solution is not acceptable for me.

Related

Nested ListView in PyQt5 and QML

I need to create nested ListView. For example,
->Item1
-->Subitem1
-->Subitem2
->Item2
-->Subitem1
I have a dictionary data inside Python side. I need to display this data from QML ListView.
So, I should take the model from python and I should design collapsable ListView inside QML. I searched about this but I couldn't find example about it. I tried to create my own model but it didn't work.
Can someone help me?
Take a look at sections in ListView
https://doc-snapshots.qt.io/qt5-5.11/qml-qtquick-listview.html#section-prop

Can we add multiple models and export it to qml?

I have created a model and exported it to qml. Now qml list elements gets updated from this model but I need a nestedlist. On click of the outerlist element inner list gets opened and the innerlist is a separate model. Any examples ?
You can achieve that by using the TreeView control. There are a few examples in the Examples section of Qt Creator.

Can I use a custom widget as a view in a model-based QListView?

I would like to render a series of complex data in a scrollable list using Qt5. Since the source of the data is a timeline, I'd like to load it lazily --- that is, I would like to use the features of QAbstractItemModel to load data on demand. The views will be read-only.
Can I use a custom widget to display data in each cell of the list?
So far I've seen some suggestions:
use QAbstractItemView->setIndexWidget(); however, because I'd like to load the data lazily, it seems needlessly expensive to create and set widgets for all indices, before the data is loaded.
use QAbstractItemView->setItemDelegate() with a custom QStyledItemDelegate that overrides paint(). The result looked good, but the widgets were simply rendered, and not interactive (couldn't select text, etc).
You can do it with QGraphicsScene or even with QScrollArea + your custom widgets. It is not necessary to use QAbstractItemModel everywhere.
If you will use custom widgets for each model item you, probably, will have performance and interactivity problem.
Ofc, you can write a custom delegate, but delegates with interactivity is very complex topic - it is necessary to handle mouse events manually, draw a selection, etc.

QML Nested List View with separators

I'm trying to implement a tree view that represent structure of buildings/floors/rooms. All rooms should be classified by floor and building ( thus header for each building and floor is required.
First I implemented it with Repeater, Row and Grid elements. However i need a selection behaviour so i need to use ListView and GridView. However I have problems displaying nested list views.
Here's my code so far: http://pastie.org/private/3seqntgvskbyxnmnuluaua
Expected Result( when using Grid and Repeater instead of GridView and List View ):
Haven't tried it myself, but here is how to implementation a tree view in QML.
http://www.codeproject.com/Articles/632795/QML-TreeModel-and-TreeView
There is still no official tree view component in Qt (as of version 5.2). However, see the solution provided by jens on this thread
http://qt-project.org/forums/viewthread/30521
This is by far the best tree view that I have come across. It should provide a good basis for what you are trying to do.
Looks like Qt Company is releasing a QML TreeView in Qt5.5. Alpha release should be coming out in February.
http://blog.qt.io/blog/2015/02/05/licensing-of-new-modules-in-qt-5-5/

loading QICon asynchronously in QListview

I am using a delegate to show a standard item model data into a listview in qt.
The model has a QIcon and a header and subheader strings.
Now i want to load remote images via http into the listview asynchronously.
How can i do this?
I have already got the listview running.
There are a lot of different ways to solve your task, and correct answer depends on how you're building your ui and what it's meant for and how other components in your application work. Nevertheless, here's an idea for you to consider:
Create a model for your listview, a guess each item of your model should have a link to the icon\image which you're going to download.
Use QNetworkAccessManager to connect to asynchronously download the image via http. Example of you can do this is here: qt networkManager get
Once an image is downloaded network manager's "finished" signal handler will be called, there you need to update a record in your model corresponding to the downloaded link with the pointer to the image object.
For the list you can use an item delegate to draw an empty place holder (or just a default image) for the records which don't have an icon downloaded yet;
Make sure you're synchronizing your model (QMutex QSemaphore QWaitCondition)
hope this helps, regards

Resources