Search code examples
qtqtablewidgetqtablewidgetitem

Qt, How can I sort QTableWidget column if the cells are widgets


I have a QTableWidget, some columns are filled with text, some with numbers, these columns are fine to sort. But I also have a column with custom widgets, how should I enable sorting for these?

My first thought was of course to overload the '<' method on the QTableWidgetItem, but there are no QTableWidgetItem. So what would be the best way to solve this?


Solution

  • QTableWidget is not ideal for this case, consider using QTableView. Anyway, I will show you how to sort a QProgressBar widget in a QTableWidget.

    As you already said, you can overload the <() operator for this case. Let's say you have a QProgressBar in column number 4. You have to overload the <() operator.

    You have to subclass QProgressBar and QTableWidgetItem.

    class CustomTableWidgetItem : public QProgressBar, public QTableWidgetItem
    {
        Q_OBJECT
    public:
        CustomTableWidgetItem( QWidget* parent ) : QProgressBar( parent )
        {
        }
    
        CustomTableWidgetItem(const QString txt = QString("0"))
            :QTableWidgetItem(txt)
        {
        }
    
        bool operator <(const QTableWidgetItem& other) const
        {
             if(other.column() == 0 /* numeric cell */) {
                 return QTableWidgetItem::text().toInt() < other.text().toInt();
             }
             else if(other.column() == 4 /* progress bar */) {
                const QProgressBar *p = dynamic_cast<const QProgressBar *>(&other);
                if(p != 0) {
                    if(this->value() < p->value())
                        return true;
                }
            }
    
            return false;
        }
    };
    

    And then you can insert your QProgressBar like this in cell number 4.

    Insert QProgressBar like this

    ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    CustomTableWidgetItem *pgbar = new CustomTableWidgetItem(this);
    ui->tableWidget->setCellWidget(ui->tableWidget->rowCount()-1, 4, pgbar);
    ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 4, pgbar);
    

    Let's say you want to have simple text at cell 1 use QTableWidgetItem.

    ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 1, new QTableWidgetItem("Hello"));
    

    If you want to sort numbers as well, for example in cell 0 use CustomTableWidgetItem, since we have implemented an if-statement as you can see above for sorting numbers and progressbar.

    ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0, new CustomTableWidgetItem(QString::number(ui->tableWidget->rowCount())));
    

    You can use this approach with other widgets as well, just subclass the correct widget, but in general it's better to use QTableView (MVC approach).

    Here is a screenshot with QTableWidget and QProgressBar sorting.

    enter image description here