gpt4 book ai didi

c++ - 如何在 SQLite3 中使用 Qt QSqlDriver::subscribeToNotification?

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:20 25 4
gpt4 key购买 nike

我正在编写一个 Qt 应用程序,其中不同的模型可以插入/删除/更新同一个表。当一个模型更改数据库时,我希望其他模型能够收到更改通知,以便他们可以相应地更新 View 。

在 SQLite 中监控插入、删除和更新的最佳方法似乎是使用 QSqlDriver::subscribeToNotification 然后对通知信号使用react。我知道语法是这样的:

db.driver()->subscribeToNotification("anEventId");

但是,我不确定 anEventId 是什么意思。 anEventId 是 SQLite 提供的常量,还是我使用触发器或其他方式将这些特定事件编码到 SQLite 中,然后订阅它们?

最佳答案

Qt sqlite 驱动程序中的 subscribeToNotification 实现依赖于 sqlite3_update_hook sqlite C API 的功能。然而,Qt 驱动程序并没有转发执行的操作,只是转发表名,这应该是传递给 subscribeToNotification 的神秘的 anEventId 参数>。简而言之,您可以监听任何表中发生的事件(假设它是一个 rowid table ,但它通常是)将表名传递给 subscribeToNotification 方法。但是,当您的槽捕捉到 notification 信号时,您只知道该表中发生了一个操作,但(遗憾的是)Qt 不会告诉您是哪一个(INSERT、UPDATE 或 DELETE)。

因此,给定一个 QSqlDriver * 驱动程序:

driver->subscribeToNotification("mytable1");
driver->subscribeToNotification("mytable2");
driver->subscribeToNotification("mytable3");

然后在你的位置:

void MyClass::notificationSlot(const QString &name)
{
if(name == "mytable1")
{
// do something
}
else if(name == "mytable2")
{

//etc...

关于c++ - 如何在 SQLite3 中使用 Qt QSqlDriver::subscribeToNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com