为什么 INSERT 和 DELETE 会搞砸 QTableView(Qt、C++、sqlite)?

发布于 2024-10-17 13:17:45 字数 186 浏览 2 评论 0原文

在 QSqlQueryModel 上设置 INSERT 或 DELETE 查询后,我的 QTableView 变得一团糟。例如,我通过调用 view->hideColumn(ID); 隐藏了 ID 列,但在 INSERT 或 DELETE 后,ID 列变得可见。

在这些情况下,如何自动将视图重置为之前的设置?

After setting an INSERT or DELETE query on QSqlQueryModel, my QTableView becomes screwed up. For example I hid the ID column by calling view->hideColumn(ID); but after an INSERT or DELETE the ID column becomes visible.

How can I automatically reset my view to the previous settings in these cases?

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

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

发布评论

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

评论(1

明明#如月 2024-10-24 13:17:45

我想问题出在 QSqlQueryModel::setQuery 你最终调用每次重新加载内容和插入\删除行时。看看 setQuery 实现,我建议:根据查询,您的模型可以重置,包括列设置更改,这应该触发视图列更新。

正如 Qt 文档所示:

QSqlQueryModel 类提供了
SQL结果的只读数据模型
套。

所以我会使用直接 QSqlQuery 调用进行数据更新,然后重新加载模型使用相同的查询。或者考虑切换到 QSQLTableModel,这对于单个表内容操作非常方便并且支持插入更新和删除。看看下面的示例是否适合您:

设置数据库、视图和模型:

QSqlTableModel *_model;
QTableView *_view;

...

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open() ;

QSqlQuery query;
query.prepare("CREATE TABLE IF NOT EXISTS person (id INTEGER UNIQUE PRIMARY KEY, name VARCHAR(30))");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ('test1')");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ('test2')");
query.exec();

_model = new QSqlTableModel(this, db);
_model->setTable("person");
_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
_model->select();
_model->setHeaderData(1, Qt::Horizontal, tr("name"));
_model->setSort(1, Qt::AscendingOrder);

_view = new QTableView(this);
_view->setModel(_model);
_view->hideColumn(0);

添加新行:

QSqlRecord record;
_model->insertRecord(-1, record);

删除所选行:

QModelIndexList selected = _view->selectionModel()->selectedIndexes();
for (int i = 0; i < selected.size(); ++i)
    _model->removeRows(selected.at(i).row(), 1);

提交更改:

_model->submitAll();

希望这有帮助,问候

I guess the problem is in QSqlQueryModel::setQuery you're eventually calling every time content gets reloaded and rows inserts\deletes. Looking at the setQuery implementation I would suggest: depending on the query your model can be reset including columns settings change which should trigger view columns update.

As Qt documentation suggests:

The QSqlQueryModel class provides a
read-only data model for SQL result
sets.

so I would use direct QSqlQuery calls for the data updates and then would reload the model with the same query. Or consider switching to QSQLTableModel, which is quite handy for single table content manipulation and supports inserts updates and deletes. See if an example below would work for you:

set up database, view and model:

QSqlTableModel *_model;
QTableView *_view;

...

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open() ;

QSqlQuery query;
query.prepare("CREATE TABLE IF NOT EXISTS person (id INTEGER UNIQUE PRIMARY KEY, name VARCHAR(30))");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ('test1')");
query.exec();
query.prepare("INSERT INTO person (name) VALUES ('test2')");
query.exec();

_model = new QSqlTableModel(this, db);
_model->setTable("person");
_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
_model->select();
_model->setHeaderData(1, Qt::Horizontal, tr("name"));
_model->setSort(1, Qt::AscendingOrder);

_view = new QTableView(this);
_view->setModel(_model);
_view->hideColumn(0);

add new row:

QSqlRecord record;
_model->insertRecord(-1, record);

delete selected row(s):

QModelIndexList selected = _view->selectionModel()->selectedIndexes();
for (int i = 0; i < selected.size(); ++i)
    _model->removeRows(selected.at(i).row(), 1);

submit changes:

_model->submitAll();

hope this helps, regards

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