如何检测表视图中的项目何时被修改?

发布于 2024-12-04 11:23:14 字数 911 浏览 0 评论 0原文

我有这个表视图,其中我在 3 列上添加了不同的项目。这些项目是可编辑的,因此我可以直接在视图中修改它们。

    bool ClothoidTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (index.isValid() && role == Qt::EditRole) {
            int row = index.row();

            ClothoidCurve p = listOfCurves.value(row);

            if (index.column() == 0)
                p.length = value.toFloat();            
            else if (index.column() == 1)
                p.startCurvature = value.toFloat();
            else if (index.column() == 2)
                p.endCurvature = value.toFloat();
            else
                return false;

            listOfCurves.replace(row, p);
            emit(dataChanged(index, index));

            return true;
        }

        return false;
    }

上面的方法是在我的表模型中声明的,当我添加和修改表中的数据时都会调用它。

我想仅在修改表中的项目时发送信号。我该怎么做?有什么方法可以区分添加和修改吗?

I have this table view in which I add different items on 3 columns. The items are editable so I can modify them directly in the view.


    bool ClothoidTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if (index.isValid() && role == Qt::EditRole) {
            int row = index.row();

            ClothoidCurve p = listOfCurves.value(row);

            if (index.column() == 0)
                p.length = value.toFloat();            
            else if (index.column() == 1)
                p.startCurvature = value.toFloat();
            else if (index.column() == 2)
                p.endCurvature = value.toFloat();
            else
                return false;

            listOfCurves.replace(row, p);
            emit(dataChanged(index, index));

            return true;
        }

        return false;
    }

The method above is declared in my table model and it is called both when I add and when I modify the data in the table.

I would like to send a signal only when I modify the items in the table.How could I do that? Is there any way to differentiate between addition and modification?

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

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

发布评论

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

评论(1

迷你仙 2024-12-11 11:23:14

您想如何以及在哪里了解差异?行为良好的模型会发出 rowsAboutToBeInsertedrowsInserted。我认为(尽管我不确定)新行的数据设置应该在这些调用之间进行。无论如何,值得一试。否则,您也许能够跟踪最后插入的行,并使用它来区分“添加”和“编辑”。它并不完美,但可能涵盖了您的大部分用例。

How and where do you want to know about the difference? Well-behaved models emit rowsAboutToBeInserted and rowsInserted before and after new data is added. I would think (although I'm not sure) that the setting of data for the new rows should happen between these calls. It's worth a shot, anyway. Otherwise, you might be able to track the rows that were last inserted, and use that to distinguish between "adding" and "editing". It would be imperfect, but probably cover most of your use cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文