QTableWidget信号cellChanged():通过例程区分用户输入和更改
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在类似的情况下,我只是
在设置表之前使用了阻止信号,然后取消阻止:
In similar situation I've just used
Block signals before setting up the table, then unblock:
看来,这是 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.
我建议从
QTableWidget
更改为具有适当模型的QTableView
。从它的声音来看,无论如何,您都有一个数据库或其他数据对象来保存和排列数据,因此希望这很容易做到。这样您就可以区分编辑(在模型上调用setData
)和更新(在模型上调用data
)。I would recommend changing from a
QTableWidget
to aQTableView
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).