I have a QTableView whose model is a QStandardItemModel. In one of the columns, I create new indexes and use setIndexWidget to assign them to a custom color-selection widget. I'd like to be able to sort the view using this column so users could group items with like colors. I've implemented the operator< for the custom widget, but whenever I try to use it I get a run-time debug assertion saying:
Expression: Invalid operator<
In order to sort by this column do I need to set some role data for the QStandardItem the widgets are attached to?
I think the operator< code is fine but I'm including it as well just in case.
bool operator<(const QPenWidget &rhs) const
{
// use the RGB value to index the color
int r_lhs, g_lhs, b_lhs, rgb_lhs;
int r_rhs, g_rhs, b_rhs, rgb_rhs;
this->m_pen.color().getRgb(&r_lhs, &g_lhs, &b_lhs);
rgb_lhs = (r_lhs * 65536) + (g_lhs * 256) + b_lhs;
rhs.m_pen.color().getRgb(&r_rhs, &g_rhs, &b_rhs);
rgb_rhs = (r_rhs * 65536) + (g_rhs * 256) + b_rhs;
if (rgb_lhs != rgb_rhs)
return rgb_lhs < rgb_rhs;
if (m_pen.style() != rhs.m_pen.style())
return m_pen.style() < rhs.m_pen.style();
return m_pen.width() < rhs.m_pen.width();
}
By default, the
QStandardItemModeluses whatever the data for theQt::DisplayRoleis to do the sorting. While it's possible to change the role usingsetSortRole, for index widgets the easiest thing to do is put some sorting hash value into the display role (note: this doesn't affect how the widget is shown in the view).For this application here is a hash function:
Then when creating the index: