Reputation: 53
I'm new, I'm learning to program in Qt and my English is not very good, my problem is that when I update a cell in a QTableView to use its value in another cell, it uses the previous value and not the new, I show them the code as I am doing, thanks.
bool MainWindow::eventFilter(QObject * watched, QEvent * event)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
qDebug() << ke->type();
if(ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
{
int fila = ui->tableView->currentIndex().row();
int col = ui->tableView->currentIndex().column();
double valor1 = ui->tableView->model()->data(ui->tableView->model()->index(fila,1)).toDouble();
double valor2 = ui->tableView->model()->data(ui->tableView->model()->index(fila,3)).toDouble();
if(col == 1 || col == 3)
{
ui->tableView->model()->setData(ui->tableView->model()->index(fila,col + 1),2.0*valor1);
ui->tableView->model()->setData(ui->tableView->model()->index(fila,col + 3),200.0*valor1/valor2);
}
}
}
return false;
}
Upvotes: 4
Views: 21141
Reputation: 10495
If you are inside a custom data model, (perhaps inheriting from QAbstractTableModel
, since we're discussing QTableView
s), you can inform the view that a change of data has occurred by emitting the QAbstractItemModel::dataChanged() signal.
Here's how I do it.
QModelIndex startOfRow = this->index(row, 0);
QModelIndex endOfRow = this->index(row, Column::MaxColumns);
//Try to force the view(s) to redraw the entire row.
emit QAbstractItemModel::dataChanged(startOfRow, endOfRow);
QModelIndex startOfColumn = this->index(0, mySpecificColumn);
QModelIndex endOfColumn = this->index(numRows, mySpecificColumn);
//Try to force the view(s) to redraw the column, by informing them that the DecorationRole data in that column has changed.
emit QAbstractItemModel::dataChanged(startOfColumn, endOfColumn, {Qt::DecorationRole});
By adding convenience functions like UpdateRow(row) and UpdateColumn(column) to your item model, you can call those functions from outside of the model itself, if you change the data externally.
You don't want to tell a view to update itself - what if there are more than one view looking at the same model? Let the model to inform all the attached views that it has changed.
Upvotes: 6
Reputation: 53
This is the code that I use if anyone had the same problem.
connect(ui->tableView->model(),SIGNAL(dataChanged(QModelIndex,QModelIndex)),SLOT(UpdateData(QModelIndex,QModelIndex)));
void MainWindow::UpdateData(const QModelIndex & indexA, const QModelIndex & indexB)
{
int col = indexA.column();
int fila = indexA.row();
if(col == 1 || col == 3)
{
double valor1 = ui->tableView->model()->data(ui->tableView->model()->index(fila,1)).toDouble();
double valor2 = ui->tableView->model()->data(ui->tableView->model()->index(fila,3)).toDouble();
ui->tableView->model()->setData(ui->tableView->model()->index(fila ,col + 1),2.0*valor1);
ui->tableView->model()->setData(ui->tableView->model()->index(fila,col + 3),(200.0*valor1/valor2));
}
}
This will update the cell value that depends on another cell that has been updated.
Upvotes: 1
Reputation: 3449
This seems to be quite ditry approach to do this for several reasons:
Upvotes: 0