QTableWidget信号cellChanged():通过例程区分用户输入和更改

发布于 2024-08-25 05:53:57 字数 407 浏览 5 评论 0原文

我正在使用 PyQt,但我的问题是一般的 Qt 问题:

我有一个由 updateTable 函数设置的 QTableWidget。调用时它将数据从 DATASET 写入表中。不幸的是,这导致我的 QTableWidget 为每个单元格发出信号 cellChanged() 。

信号 cellChanged() 连接到函数 on_tableWidget_cellChanged,该函数读取已更改单元格的内容并将其写回 DATASET。这是允许用户手动更改数据所必需的。

因此,每次更新表时,其内容都会写回 DATASET。

有没有办法区分单元格是由用户更改还是由 updateTable 更改?

我想暂时通过 updateTable 断开 on_tableWidget_cellChanged 的​​连接,但这似乎有点脏。

i am using PyQt but my question is a general Qt one:

I have a QTableWidget that is set up by the function updateTable. It writes the data from DATASET to the table when it is called. Unfortunately this causes my QTableWidget to emit the signal cellChanged() for every cell.

The signal cellChanged() is connected to a function on_tableWidget_cellChanged that reads the contents of the changed cell and writes it back to DATASET. This is necessary to allow the user to change the data manually.

So everytime the table is updated, its contents are written back to DATASET.

Is there a way to distinguish if the cell was changed by the user or by updateTable?

i thought of disconnecting on_tableWidget_cellChanged by updateTable temporarily but that seems to be a little dirty.

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

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

发布评论

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

评论(3

温馨耳语 2024-09-01 05:53:57

在类似的情况下,我只是

bool QObject::blockSignals ( bool block )
bool QObject::signalsBlocked () const

在设置表之前使用了阻止信号,然后取消阻止:

myTable.blockSignals(True)
#blah-blah..
myTable.blockSignals(False)

In similar situation I've just used

bool QObject::blockSignals ( bool block )
bool QObject::signalsBlocked () const

Block signals before setting up the table, then unblock:

myTable.blockSignals(True)
#blah-blah..
myTable.blockSignals(False)
居里长安 2024-09-01 05:53:57

看来,这是 QTableWidget 中唯一的信号,至少在 4.6 版本中是这样。您可以发布功能请求,但我不知道它是否被接受,您可能会等待很长时间;-)

也许您可以尝试编写 QTableWidget 的子类并在单元格内部更改时发出自己的信号。

无论如何,在更新单元时断开连接并不是那么糟糕,因为您无法连接到特定信号。

It seems, that this is the only signal in QTableWidget at least for 4.6. You could post a feature request, but I don't know if it is accepted and you might wait for long time ;-)

Maybe you could try to write a subclass of QTableWidget and emit own signals, when cell is changed internally.

Anyway, disconnecting for the time of updating the cell isn't that bad, since you can't connect to specific signal.

隐诗 2024-09-01 05:53:57

我建议从 QTableWidget 更改为具有适当模型的 QTableView。从它的声音来看,无论如何,您都有一个数据库或其他数据对象来保存和排列数据,因此希望这很容易做到。这样您就可以区分编辑(在模型上调用 setData)和更新(在模型上调用 data)。

I would recommend changing from a QTableWidget to a QTableView with an appropriate model. From the sounds of it, you have a database or other data object holding and arranging the data anyway, so it would hopefully be fairly easy to do. This would then allow you to distinguish between edits (setData is called on your model) and updates (data is called on your model).

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