QTableView Sorting is not Properly.
-
Dear All,
I have A QTableView with different columns and I have a QComboBox with items as QTableView column name.what i want like when we change the column name from comboBox then it should sort the items in Ascending Order.so for this the code is for sorting ism_sortModel = new QSortFilterProxyModel; m_tableView = new QTableView(this); m_sortModel->setSourceModel(new QAbstractTableModel); m_tableView->setModel(m_sortModel); m_tableView->setSortingEnabled(true);
The above code is sorting the item but not the proper way what i want. Like if the item inside the column is 1,2,3,10,11,3 then it will sort like 1,10,11,2,3,3. so thats the problem.
what i want it should sort like 1,2,3,3,10,11.
Please if you have any idea or suggestion.
TIA -
Your model sorts by string instead of by value. Make sure you convert the values to int when sorting.
-
Hi @Sebastian,
for ex:- string(1,2,3,10,11,3 )
convert the string to list of integer
QList<int> s = {1,2,3,10,11,13}
Sort the list:- s = {1,2,3,3,10,11}
Convert it to string again then set it to model. -
new QAbstractTableModel
this can't compileMake sure you convert the values to int when sorting.
I disagree, you should store the values as numbers and convert them to string only if you absolutely, positively need them as string.
I suspect you are having the same problem as this nice chap
-
@VRonin said in QTableView Sorting is not Properly.:
I disagree, you should store the values as numbers and convert them to string only if you absolutely, positively need them as string.
Yes, agreed.