当从我的应用程序外部更改基础SQL表时,自动刷新QSqlTableModel / QTableView [英] Auto-refresh QSqlTableModel/QTableView when the underlying SQL table is changed from outside of my application

查看:2537
本文介绍了当从我的应用程序外部更改基础SQL表时,自动刷新QSqlTableModel / QTableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QSqlTableModel QTableView 显示来自底层SQLite数据库的表。同时,该表可以在我的应用程序之外更改。当发生这种情况时,我需要检测这个并刷新 QSqlTableModel QTableView

I use QSqlTableModel and QTableView to display a table from an underlying SQLite database. At the same time, the table can be changed outside of my application. When that happens, I need to detect this and refresh the QSqlTableModel and QTableView.

目前, QSqlTableModel QTableView 根本不检测更改,直到某些用户动作强制刷新,例如编辑字段。那么我该如何实现呢?

Currently, the QSqlTableModel and QTableView do not detect the changes at all, until some user action forces a refresh, such as editing a field. So how can I implement this?

现在我正在考虑轮询,但我不知道如何轮询更改,特别是如何做最小开销。我只是运行一个 COUNT SQL查询?如果添加了元素但又删除了一个元素,该怎么办?如果表没有改变,我宁愿不强制刷新。

Right now I'm thinking about polling, but I'm not sure what to poll for changes, and especially how to do it with minimum overhead. Do I just run a COUNT SQL query? What if an element has been added but another removed? I'd rather not force-refresh if there are no changes to the table.

那么最好的方法是什么呢?

So what would be the best way?

推荐答案

也许它会对你有用。尝试使用 QFileSystemWatcher 检测任何文件更改。

Maybe it will be useful for you. Try to use QFileSystemWatcher to detect any file changes.

    QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
    watcher->addPath("path");
    connect(watcher,SIGNAL(fileChanged(QString)),SLOT(updateData()));

插槽:

void QSSViewer::updateData()
{
    qDebug() << "changed";
}

我在我的电脑上测试过,当我用另一个程序更改数据库时, code> QFileSystemWatcher 捕获此更改每次。因此,您可以在 updateData()插槽中执行所有必需的操作。

I tested it on my computer and when I change database with another program, QFileSystemWatcher catched this changes every time. So you can do all needed actions inside updateData() slot.

这篇关于当从我的应用程序外部更改基础SQL表时,自动刷新QSqlTableModel / QTableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆