Automatically sorting on insert in QTreeWidget - c++

I have a QTreeWidget that I insert items in, and the user can select a column to sort it. As the items are being inserted, they just get appended to the end instead of having the sort being done automatically. If I click the header to switch between ascending/descending it will sort the current items.
I figured I could call sortItems() and use the column that is returned from sortColumn(), but I am unable to see how I can see if the user is doing an ascending or descending sort.
I'm not worried about the efficiency of this, so I don't want to wait until the insertions are done and then do the sort. I'd like a real-time sorted list.
Thanks!

If your tree widget is called treeWidget, you should be able to call the header() method, which is from QTreeWidget's parent QTreeView, then sortIndicatorOrder() from the QHeaderView class:
treeWidget->header()->sortIndicatorOrder()
With this, you know the user's current sort order, and you can apply your sort on insert according to this.

I don't have a setup for testing but according to the documentation, this should cause sorting to occur as items are inserted.
...
treeWidget.sortByColumn(0, Qt::AscendingOrder); // column/order to sort by
treeWidget.setSortingEnabled(true); // should cause sort on add
Qt recommends against doing this as there will be a performance cost and say that you should set sorting enabled after all the adds are completed. Hope this helps.

Related

How to force sorting a QTableWidget prior to displaying

I'm using Qt 5.21.3. I have a QTableWidget that, when initially displayed, shows the rows in the order they were populated. I've set setSortingEnabled(true) and all rows are sorted properly when I click the column headers, but I'd like the table sorted on a specific column, in this case column 0, before showing the table but I can't for the life of me figure out how to accomplish it.
I've tried setting setSortingEnabled(false) prior to populating the table then to true before showing the table, but that appears to do nothing.
The way I'm getting around this now is to use std::sort to sort QList before populating the table, but this just seems like kludge to me and I'm not sure I can sort on an item other than the first if I need to.
You have to invoke the sort() method of the model:
tableWidget->model()->sort(0, Qt::AscendingOrder);
or sortItems() method:
tableWidget->sortItems(0, Qt::AscendingOrder);
after filling the table.

How to design MFC CListCtrl refresh operation?

I have the following small issue. A pretty old MFC application being ported to C++11. There is a MFC CListCtrl in report view the data in which is being refreshed by a separate thread. Either manually or via a timer. The current implementation sets m_ListCtrl->SetRedraw(FALSE); deletes all items then populates them back again. This causes a flicker. Also internally the items are sorted via GetItemData DWORD, setting each list items new position after sorting. The state of sorting is internal to CListCtrl.
What is the best design pattern to add new, update or remove deleted list items after the table has been modified? I am thinking of hashing the contents of the list item row, keeping a separate vector<RowObject> table current and after refresh and comparing hashes. Is this a right approach?
Or may be use m_ListCtrl->SetItemText on existing items? But how to deal with sorting in this case?

Qt QTreeWidget - disabling interaction for a single column

is there a way to disable interaction or just sorting for a specific row in a QTreeWidget? I have a table where if you click the header, it sorts the items. I have a custom "CTreeWidgetItem" where I have overloaded the < operator to perform a custom sort on my data. The problem is, it sorts on every header I click, not just the first column. I need it to only work for the first column. Is there something I can do?
Simply connect to the sectionClicked() signal of the header (QTreeView::header()). Then force the sort indicator back to the first section QHeaderView::setSortIndicator().

Flex 3 MX List: need to programmatically reorder the list's item renderers

Ok, I have to use Flex 3 because I am using this in an Adobe Connect pod. I have a List component, which is capable of being reordered through drag and drop. If someone leaves the browser and comes back into the meeting room, I am trying to redisplay how they had items ordered from their drag and drop. So I need a way to reorder the list. I was looking into sort for Arrays on the dataProvider. But I have not been able to figure out the proper event to sort the list once, all of the items have the appropriate data in.
In any case, does anyone know how to tell the data of an itemRenderer to have ordered values, and then tell the list to reorder the items in the list according to the new values?
One of the possible ways I used:
Keep your data in Object(map) with key:value, Object{rowIndex:rowData}
e.g. {1 : row1Data,
2 : row2Data, ... }
Prepare the list values based on the keys and then assign it to the grid. This way, Itemrenderer will no longer need to know the "order" of the data. Their task is just to display the data.
Once user is done with drag-drop - update the map, persist it.
Hope this helps.
If you use an ArrayCollection, you can apply a sort, and then call arrayCollection.refersh() to refresh the collections with the sort, which will then update the list display

TFS 2010 sorting ALLOWED VALUES desc

I'm using TFS 2010.
I have a string field with a list of allowed values. When I view the work item the list is sorted alphabetically (the original list is not sorted alphabetically).
Is there any way to display the vaule order as the same given in the work item template.
Is there any way to change the sort order to desc?
I know I can write a custom control for this but I was wondering if there was any easier way.
thanks,
There is no way to sort a list of values (allowed, suggested, or even taken from a global list) in any order, but ascending alphabetically.
You do have two option though:
Add an index before each item on the list. This index would be something like "1. Banana", "2. Apple", etc. It would be permanent and would appear in the display of the list, but it is the simplest solution, if it's good enough for you.
You may create a custom control, that would appear as a drop down list, and would sort by a custom parameter that you can add to the work item type definition.
Hope this helps.