I made a new class named PageRangeListWidgetItem which inherits from QListWidgetItem
class PageRangeListWidgetItem : public QListWidgetItem
{
...
private:
int start, end;
bool operator<(const PageRangeListWidgetItem &other) const {
if (start == other.start)
return end < other.end;
return start < other.start;
}
};
and these items are being added to a QListWidget through user input. The QListWidget contains only these PageRangeListWidgetItem’s, so I hoped that by calling the sortItems() function of the QListWidget, that it would use the overloaded operator< to sort the items to my liking, but unfortunately it doesn’t, it keeps on sorting the list as if it contained pure QListWidgetItems.
How can I change this behaviour? Do I have to create a custom QListWidget class or is there an easier way?
The
<function that is called by Qt’s framework isThe one you have defined does not match this function signature (
PageRangeListWidgetIteminstead ofQListWidgetItem), and thus does not override the virtual function and doesn’t get called.You need to write a
<function that takes in aQListWidgetItemand uses that object to sort rather than your custom object.