Reordering Qt Listview via drag'n'drop - qt

I have tried the approach described in http://agateau.com/2016/reordering-a-listview-via-dragndrop-3/ to implement drag'n'drop support in a listview.
However, when the amount of items in the list grow there is a problem when scrolling while dragging.
To reproduce the problem, download project from https://github.com/agateau/listviewdragitem/tree/3-placeholders. Triple the amount of items in the list, and try to drag an item (all but first) to the end of the list. The dragged tem will dissapear when the list has scrolled for some time. I have not noticed any signals beeing emitted indicating that the drag has completed.
The first item can be moved correctly for some reason.
Do you have any suggestions what may cause this behavior?

The reason, that the first Item does not disappear is, that it is the currentItem which is protected from deletion.
The reason that the other Items suddenly disappear is, that the ListView instantiates and destroyes the Items as it thinks they are visible. This means: As they would be visible on their original position.
Set the currentIndex to the index of the Item that is being dragged, to prevent it from being destroyed.
Also you could use a DelegateModel and add relevant Items to the persistent group, to prevent destruction.

Related

Check if QML Item is being drawn

I have a set of QML items distributed all over my UI. They display data from a remote device and their content needs to be updated regularly. The Items are spread on several tabs and hidden in nested ListView instances, so most of them won't be visible to the user all the time.
In order to keep the bandwidth low I want to update only those items that are currently visible to the user.
I am looking for the right hook to get the information which of these Items is currently displayed from within the Item, without relying on information from the parents. If they were all placed in ListView delegates I could use the delegate's Components onCompleted and onDestroyed signals. Since this is not the case I am stuck at finding out how to get this information.
Am I missing something here? Is there an onPaintFinished signal or something similar? My workaround would be to add that logic to the parent containers, but that would be tedious, since there are several kinds of container that can contains these display Items.
Instances that are on delegates of a ListView will not exist until they would be in the visible range or the cache range around the visual area of the list view. If the delegate moves outside of that range, it is destroyed. So, no need to worry about instances hidden there.
Furthermore, items are currently not visible are also not drawn. They are not entered into the scene graph, and hence, not rendered. So, instances of your items appearing on tabs that are currently not current will also not be drawn. However, these items do still exist of course.
Figuring out if an item is effectively visible or not is quite a hard problem though. QML delegates part of that to OpenGL (clipping for instance). There is not feedback on the result of that. You could in theory lift that information out of the renderer, but that would require customizing that and that is very hard. You could take a look at the heuristics that GammaRay uses to warn about items not being visible. Perhaps you can take some inspiration from that.

Prevent drag outside QTreeWidget

In a QTreeWidget I want to be able to reorder items using InternalMove AND receive drops from another tree in my application.
If I set dragDropMode to InternalMove I cannot drop items inside it. However, if I set it to DragDrop it lets the user drag items out of the my QTreeWidget.
Is there a way of preventing this? Is there another way around the problem?
If you are not worried about drags outside of your application, maybe setting the DragDropMode of the other QTreeWidget in your application (or anything that derives from QAbstractItemView for that matter) to:
treeWidget->setDragDropMode(QAbstractItemView::DragOnly);
is enough to get the desired behavior. This does not prevent the items of the QTreeWidget in question from being dragged, but they cannot be dropped in the other tree, while you can still drop them (and the items from the DragOnly tree) into the tree that accepts drops. Does this help? (Admittedly not the most intricate way to attack the problem but performing some local tests it seemed to work)

How to update a QLayout and get the new dimensions before returning?

This is driving me nuts. I have a custom menu class that, when set visible, shows a list of items located in a particular folder. When a hardware button is pressed, my application gets the latest list of items, populates the menu with them, and returns.
The menu displaying these items uses a QListWidget filled with custom widgets. Each of the widgets contains one or more QLabels in a horizontal layout, and is created at the time the menu is shown. In order to adjust the text displayed based on the menu width available, I need to get the size of the QLabel AFTER it has been resized according to the layout, but before the menu becomes visible to the user. The problem is, my layout does not get updated until all of the functions constructing my list return.
I have tried QApplication::ProcessEvents() and the layout update functions, but none of them have updated the values of my QLabels before returning. I can set a QTimer when the button is initially pressed, and have it show the menu, update the items, and stop itself, but that seems like a terrible solution.
Any help would really be appreciated! I've spent most of a day on this.
Marlon
I had this exact problem and could not find an answer anywhere on the Internet. Calling Layout.update(), Layout.activate(), or widget.adjustSize() (all suggested in various places) all did not work.
I had a widget with a vertical layout that I wanted to add a QLabel to and then immediately use the size of the QLabel.
The only thing that worked reliably was
layout->addWidget(myLabel);
myLabel->show();
size = myLabel->size();
It would seem that layouts will just not recalculate until you either return from a function and allow the Qt event loop to progress or manually call show() yourself.
How to update a QLayout and get the new dimensions before returning?
Don't. You're not meant to do that. It'll drive you "nuts" because you're doing it backwards. Layout updates are handled asynchronously from the event loop. Instead of getting layout dimensions right away, set yourself up to be part of the system. Some options are:
Implement a custom widget that will interact properly with the layout, growing to fill the available width of the layout. Perhaps all you need is a size policy and a way to elide text?
Make a custom layout that takes the special properties of your use case into account.
You want to call QWidget::adjustSize() on your parent widget. This will force the layout recalculations.
Have you tried using layout()->update(); ?
I've tried many but nothing works for me on Qt 5.15.
Only invented little patch - create timer and get size after 20 msec:
QTimer::singleShot(20, this, [this]
{
const auto height = myLayout->contentsRect().height();
// ...
});

Flex TileList itemrenderer + scroll = HELL

I'm going insane over this issue. Basically, I have a TileList with a custom item renderer that has a TextInput in it. Let's say that the list can show 4 items at once, if there are 5 items and I edit the text on the first one, the fifth will be edited also. In general if an item is out of view, it will be change when I edit one that is showing.
Also, I had overriden the TileList class to expose the rendererArray property (so that I could access the texts on each renderer) but it will only return the renderers which are displayed.
Any help is appreciated. I need to know how to override this weird behaviour with itemrenderers that aren't currently displayed. Thanks.
Ok, if anyone runs into a similar issue, here is what you need to do:
First of all, avoid trying to iterate through the itemrenderers like I did. If you need a TextInput or another control on your TileList, make sure that these controls are bound to a property on your data object, otherwise off-screen items will have incorrect values since their itemrenderers will be recycled from the items that left the screen when you scrolled.
If you think it through, any requirement can be solved by iterating through the dataprovider instead of the itemrenderers.
Also, if you try to expose the rendererArray property like I did, notice that you will only be able to iterate through the itemrenderers that are currently displayed, since those that would belong to the items that are off-screen will not be created yet.
I hope this wasn't too confusing..

flex tree custom item renderer children creation

I have created a custom item renderer for the tree, i have added some children in create children function, my problem is that sometimes i need to show these children and sometimes i don't, depending on clicking on a button which also i have added at create children, the problem is that i had to create the item even if i don't want it to be visible, and removed it by making visible false, and this costs a lot of memory, i have tried to create it at buttons click listener but when scrolling the child disappears, and it may appear again if i keep scrolling up and down..
i am trying to add the child just when i need it to be visible, is this possible or i have to create it on child creation method?
Typically you do something like this with states. This way the components within the container (in this case your item renderer) are only created when the container enters the given state. The nice thing about taking this approach is that you can remain oblivious to when components need to be created/removed and let the states model handle that for you. Hope that helps.

Resources