Solved Set datatype for a column in a QStandardItemModel
-
I use a QStandardItemModel so I can populate this model on my own.
However, QStandardItemModel::setItem only accepts QStandardItems, that only be instantiated with text (QString).
The first column is a number (integer) -- sorting on this column will use string sorting instead of numerical sorting, eg.:1 10 11 2 3
..
Instead of:1 2 3 .. 10 11 ..
How do I handle this/set this up?
Thanks,
Roy -
@TheFlyingMooseMan
most straight forward way would be to use QSortFilterProxyModel on top of your current model and reimplement thelessThan()
method.
Check if the passed index has the column number and return your comparison. For all other columns call the base class implementation. -
I was hoping for a more elegant solution, but subclassing QSortFilterProxyModel and reimplemting the lessThan() method works. Thanks raven-worx!
-
@TheFlyingMooseMan said in Set datatype for a column in a QStandardItemModel:
that only be instantiated with text (QString)
Look beyond the constructor. create the QStandardItem with the default constructor and then call setData on it passing the number