Sorting is wrong when using QSortFilterProxyModel on number strings and getting wrong column text
-
I remember having the same problem. At the time, the only solution I could come up with that worked was to subclass QSortFilterProxyModel, and provide my own definition of when an item was "larger than" another item.
But if you can make the sorting use numbers instead of strings, as Franzk suggests, that should work too. But then you probably have to store the numbers as numbers in the model, not as strings.
-
Have you considered writing your own custom Item model? Or at least a custom QStandardItem subclass.
From the doc of QStandardItem :
@Reimplement data() and setData() if you want to perform custom handling of data queries and/or control how an item's data is represented.@ -
There is a SortRole property in the latest snapshot:
http://doc.qt.nokia.com/4.7-snapshot/qstandarditemmodel.html
This feature makes implementing your requirement very easy. -
define a sort role:
@const int sortRole = Qt::UserRole +1;@QStandardItemModel lets you save custom values with setData and role = sortRole. For every column you can decide whether you want to hold QString or int.
Tell the sort method what role to use for sorting: @myStandardItemModel.setSortRole(sortRole);@
Now sorting works based on the values you have saved in every cell with sortRole.
-
Hi all
well thanks for the answer . it working .
for the secound question i found the solution and it looks like this :
@QString groupID = index.model()->index(index.row(), 0, index.parent()).data(Qt::UserRole).toString();
QString groupName = index.model()->index(index.row(), 0, index.parent()).data(Qt::DataRole).toString();@but there is one more problem is how do i setData to column in index ( for example 3 ) in the selected row
this dosn't work :
@index.model()->index(index.row(), 3, index.parent()).setData(.......)@ -
You get a QModelIndex for the current cell via this method
@QModelIndex QAbstractItemView::currentIndex () @
If you really want the selected cells and not the current cell you can query the selection Model, which is available via the view.
Once you have the QModelIndex of the target cell you can call the models setData() method with it. -
The row is not enough, because you have a tree and the hierarchy is also needed.
You need to have a QModelIndex of a cell which is close to where you want to go.
If you have a QModelIndex of the parent item you can use
@QModelIndex::child ( int row, int column ) @
If you have a QModelIndex of a sibling, for example an item in the same row you would use
@QModelIndex::sibling ( int row, int column ) @