Konrad
Konrad

Reputation: 7237

QTableWidget dataChanged SIGNAL

I'm displaying the count of the items in QTableWidget in QLabel, but whenever I do some update I have to update the count manually(the label). Is there any signal that emits when I remove or add rows? I tried cellChanged and itemChanged but neither of them emits when I remove a row. There's no dataChanged signal available.

This is what I do right now:

ui->tableWidget->insertRow ( ui->tableWidget->rowCount() );
// ....
ui->lblTotalElements->setText(QString::number(ui->tableWidget->rowCount()));

Upvotes: 0

Views: 855

Answers (1)

Alex Huszagh
Alex Huszagh

Reputation: 14644

There is no signal, but there is a virtual, protected slot you can use: rowsAboutToBeRemoved. Simply subclass, override the method, emit a custom signal, and then call the base class's implementation.

An implementation to add a signal when rows are removed, storing which rows are about to be removed (a range) would look like this:

class MyTableWidget: public QTableWidget
{
    Q_OBJECT

public:
    using QTableWidget::QTableWidget;

protected slots:
    virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override;

signals:
    void removed(int, int);
};



void MyTableWidget::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
    emit removed(start, end);
    QTableWidget::rowsAboutToBeRemoved(parent, start, end);
}

Upvotes: 3

Related Questions