The problem sounds like that you havent' figured out the correct event when you should perform the calculation.
Here's at least 2 working ways:
1) Inherit QTableWidget and override protected slot:
virtual void dataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
By this way you don't need to create any extra connections manually, since thanks to the underlying model/view architecture and a virtual slot you'll get the connection automatically. But you will have to inherit QTableWidget.
2) Create a custom slot of type:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
To copy to clipboard, switch view to plain text mode
..and connect the table's model's dataChanged() signal signal to your custom slot:
connect(table->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)))
connect(table->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)))
To copy to clipboard, switch view to plain text mode
By this way you don't need to inherit QTableWidget. But you will have to create the connection manually.
Then, in both ways, you'll put your calculation code into the dataChanged() slot.
Basically the indexes topLeft and bottomRight are always the same, if you don't change the data programmatically. Meaning that the user is normally able to edit one cell at time..
I don't know if this is different depending on the selection mode and behaviour.
Anyway, the code in your slot could look something like this:
table[topleft.row()][topLeft.column()] = topLeft.data().toInt();
table[topleft.row()][topLeft.column()] = topLeft.data().toInt();
To copy to clipboard, switch view to plain text mode
Bookmarks