使用新值更新 QTableView 中的单元格

发布于 2024-10-12 06:23:27 字数 1181 浏览 4 评论 0原文

我是新人,我正在学习用 Qt 编程,我的英语不是很好,我的问题是,当我更新 QTableView 中的单元格以使用其在另一个单元格中的值时,它使用以前的值而不是新的值,我向他们展示了我正在做的代码,谢谢。

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不醒的梦 2024-10-19 06:23:27

如果您位于自定义数据模型中(可能继承自 QAbstractTableModel,因为我们正在讨论 QTableView),您可以通知视图数据已发生更改通过发出 QAbstractItemModel::dataChanged() 信号。

我是这样做的。

更新整行:

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);

更新整列,但仅更新 Qt::DecorationRole:

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});

通过向项目模型添加 UpdateRow(row) 和 UpdateColumn(column) 等便捷函数,您可以从模型本身外部调用这些函数,如果您从外部更改数据。

您不想告诉视图自行更新 - 如果有多个视图查看同一模型怎么办?让模型通知所有附加视图它已更改。

If you are inside a custom data model, (perhaps inheriting from QAbstractTableModel, since we're discussing QTableViews), you can inform the view that a change of data has occurred by emitting the QAbstractItemModel::dataChanged() signal.

Here's how I do it.

Updating an entire row:

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);

Updating an entire column, but only the Qt::DecorationRole:

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.

分分钟 2024-10-19 06:23:27

如果有人遇到同样的问题,这是我使用的代码。

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 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.

梦冥 2024-10-19 06:23:27

由于以下几个原因,这似乎是一种非常愚蠢的方法:

  1. 如果您希望在不同的列中呈现相同的值,或者您有一列依赖于另一列的值,那么您可能应该相应地设计模型提供视图。
  2. 您调用的 setData 是一个纯虚函数,在模型中实现。因此,您可能需要修改 setData 实现,而不是捕获 Enter/Return 按键并对其进行操作。因为这种方式的另一种方式是所有模型更改都通过此函数。不要忘记为您更改的索引发出 dataChanged 信号。

This seems to be quite ditry approach to do this for several reasons:

  1. If you want the same value to be presented in different columns, or you have a column that depends on the value of another column, then you should probably design accordingly the model that feeds the view.
  2. setData that you are calling is a pure virtual function, that is implemented in the model. So instead of catching Enter/Return key presses and acting on them you might want to modify the setData implementation. Because this way of another all model changes pass through this function. Dont forget to emit dataChanged signals for the indexes that you change.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文