数据库更新时如何更新QTableView? [英] How to update QTableView when database updated?

查看:51
本文介绍了数据库更新时如何更新QTableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用QSqlRelationalTableModel 从数据库中提取数据,并使用tableView 显示它.现在,当我更新我的数据库时,如何自动更新 tableView 以显示它?我知道我需要使用函数 dataChanged() 来自动进行此操作,但我不知道如何使用它?任何建议将不胜感激.

I use QSqlRelationalTableModel to extract data from database, and use tableView to show it. Now, when I update my database, how to update tableView automatically to show it? I know i need to use function dataChanged() to make this automatically, but i do not know how to use it? Any suggestion will be appreciated.

主要代码如下:

QSqlRelationalTableModel *model = new QSqlRelationalTableModel(NULL, db);
model->setTable(tableName);
model->select();
tableView->setModel(model);
tableView->show();

推荐答案

不,不需要使用dataChanged().

你只需要调用QSqlRelationalTableModel::select() 每当数据库更新时.这将从数据库重新填充模型,并自动更新使用它的视图.

You just need to call QSqlRelationalTableModel::select() whenever the database gets updated. This will re-populate the model from the database, and update the views that are using it automatically.

  • If the database is updated from within you application, you can just call model->select() after the update queries get executed in your application.
  • If the database is updated from another application, you'll have to use something like PostgreSQL's event notification system, subscribe to the notification from your application using QSqlDriver::subscribeToNotification() and call model->select() in a slot connected to the notification() signal.

您可以使用 QSqlDriver::hasFeature(QSqlDriver::EventNotifications) 检查是否支持来自您的数据库的通知.

You can use QSqlDriver::hasFeature(QSqlDriver::EventNotifications) to check if notifications from your database are supported.

这篇关于数据库更新时如何更新QTableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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