Please nominate your Qt Champions for 2021!

Sorting in QTableWidget with multiple columns - issue

  • I am using qt5.
    I have a qtablewidget with 2 columns.
    Column 1 - is a Qcheckbox where the text is a number
    Column 2 - is a regular QTableWidgetItem

    My issue is sorting column 1 - not sure how to setup
    this up. I am getting conflicting advice from deriving a class
    from QTableWidget in order to control the sort -
    but then I am not sure how to setup the sort for column 1. I need a little more guidance of how sorting works ...

    Or derive a class from QWidgetTableItem for
    the operator<() - but then not I am now sure to know what
    column I sorting.

    Any advice, guidance would be much appreciated.


  • Column 1 - is a Qcheckbox

    How do you set it to be a checkbox? there is normally no need to insert a whole widget, you just need to call tableWidgetItem->setFlags(tableWidgetItem->flags() | Qt::ItemIsUserCheckable);
    if you do that you just need to use the Qt::CheckStateRole as sort role

    The normal way to do this however is to split model and view (use QStandardItemModel + QTableView) and insert a QSortFilterProxyModel between the two to manage the sorting

Log in to reply