sort the items on QTreeWidget/QListWidget, use QTreeWidgetItem::data().toInt()
-
No, it will not work for strings or any non-numerical value. See the docs.
data returns QVariant. QVariant has comparison operators so you can sort it without converting it to anything. This will work for most standard and Qt types.
If you want to sort a custom type stored in QVariant make sure it has comparison operators (at least == and <) defined and registered. -
In that case just use sortItems.
No, converting strings (or any other data) to int (or anything else) is never a good way to sort them. Giving them a proper comparison operators (like I mentioned) and sorting them directly is the right way.
-
Returns the variant as an int if the variant has userType() QMetaType::Int, QMetaType::Bool, QMetaType::QByteArray, QMetaType::QChar, QMetaType::Double, QMetaType::LongLong, QMetaType::QString, QMetaType::UInt, or QMetaType::ULongLong; otherwise returns 0.
from the docs,
it seems that QString will work fine. -
No, it will work only for strings representing a number i.e.
qDebug() << QVariant(QString("hello")).toInt(); // prints 0 qDebug() << QVariant(QString("world")).toInt(); // prints 0 qDebug() << QVariant(QString("42")).toInt(); // prints 42
I can only repeat the third time. There's absolutely no need whatsoever to convert QVariant or QString to int just to sort it. QVariant has
operator<
so is sortable directly. For standard types (ints, doubles, strings etc.) contained in QVariant you don't need to do anything extra. Just callsortItems
. It will sort by the column you choose using string comparison of the text displayed in wanted column.Only
operator<
is required to make type sortable as most sorting algorithms only use that. If they need anything else they will express other operators in terms ofoperator<
i.e.a==b -> !(a<b || b<a)
,a>b -> b<a
. -
when i re-implement operator<, how can i sort depend on various "column" data , not only the DisplayRole data.
why i do this is i want have a QTreeWidget and a QListWidget, which have the Same contents.- i sort the QTreeWidget items by the data on a column.
- when i switch into the QListWidget, i will sort by the same data with the QTreeWidget, sort the QListWidget....so i need more than sort by the DisplayRole
-
Gosh, sorry, but it's really hard to help you because you keep changing the requirements slightly and post multiple questions asking the same thing in a different context, which changes the suggested approaches. Right now you are mixing a lot of independent concepts like sorting, data roles and QVariant conversions.
For the latest scenario (a tree and list widgets) the Q*Widget classes are not the right tool.
It sounds like you've got a single piece of data displayed differently in two views. For this create one model (eitherQStandardItemModel
or a custom one derived fromQAbstractItemModel
). Then create two proxy models based onQSortProxyFilterModel
and set them as models on aQTreeView
andQListView
. In each of the proxy models implement thelessThen
method that compares two items. The items are represented asQModelIndex
so you can access any role viadata()
member of the items.
thedata()
method returns a QVariant. Depending on the role it holds different types. Don't convert them to int or anything else. QVariant can be compared directly. If you know what kind of data it holds (e.g. QString for Qt::DisplayRole) then you can usetoString()
method of the variant and then customize yourlessThen
method using this value.