QSortFilterProxyModel is sorting only on based on string.
-
wrote on 24 Jul 2019, 05:35 last edited by
I am have TreeView in my code. Basically I insert all the data as a string only in QTTreeView.
But some string are also number. So I need to sort those columns which are having number based on number sorting not on String sorting.I have a limitataion in my code that I cannot use QVariant.
Is there any way I can write custom lessthan of QSortFilterProxyModel with which I can indentify which column is string and which column is number.
Is there any way I can tell the QTTreeView that this column contains number and this column contain string without using QVariant?
-
I am have TreeView in my code. Basically I insert all the data as a string only in QTTreeView.
But some string are also number. So I need to sort those columns which are having number based on number sorting not on String sorting.I have a limitataion in my code that I cannot use QVariant.
Is there any way I can write custom lessthan of QSortFilterProxyModel with which I can indentify which column is string and which column is number.
Is there any way I can tell the QTTreeView that this column contains number and this column contain string without using QVariant?
@Ayush-Gupta said in QSortFilterProxyModel is sorting only on based on string.:
Is there any way I can write custom lessthan of QSortFilterProxyModel with which I can indentify which column is string and which column is number.
Yes, check if QString converts to int without an error. If it does, you've got a number.
Is there any way I can tell the QTTreeView that this column contains number and this column contain string without using QVariant?
You can add a custom role to your model (you need to return it in
data()
method of your model) and check it in your filter proxy. -
wrote on 24 Jul 2019, 06:29 last edited by
@sierdzio said in QSortFilterProxyModel is sorting only on based on string.:
You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.
Can you expalin this further. How I can achieve this. May be a peice of code explaining this?
-
@sierdzio said in QSortFilterProxyModel is sorting only on based on string.:
You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.
Can you expalin this further. How I can achieve this. May be a peice of code explaining this?
@Ayush-Gupta said in QSortFilterProxyModel is sorting only on based on string.:
@sierdzio said in QSortFilterProxyModel is sorting only on based on string.:
You can add a custom role to your model (you need to return it in data() method of your model) and check it in your filter proxy.
Can you expalin this further. How I can achieve this. May be a peice of code explaining this?
If you have a custom subclass for your model (do you?), you can do this:
// In some header enum CustomRole { ColumnHasNumbers = Qt::UserRole + 1 } // In model: QVariant YourModelClass::data(const QModelIndex &index, int role) const { switch (role) { case Qt::DisplayRole: // ... case Qt::EditRole: // ... case ColumnHasNumbers: // You need to detect which column has only numbers either here (inefficient) or somewhere else, for example during initialization of the model if (columnHasNumbers) return true; else return false; }
-
wrote on 24 Jul 2019, 07:14 last edited by
how can we return true or false? Seems data should return actual data column contains
-
how can we return true or false? Seems data should return actual data column contains
@Ayush-Gupta Boolean can be converted to QVariant: https://doc.qt.io/qt-5/qvariant.html#QVariant-8
So, the code should just work. -
how can we return true or false? Seems data should return actual data column contains
@Ayush-Gupta said in QSortFilterProxyModel is sorting only on based on string.:
how can we return true or false? Seems data should return actual data column contains
Look at other roles: https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum there is far more information that can be (and is) returned than just column contents: you can specify the font, colours, size hints and custom roles.
1/7