为什么 INSERT 和 DELETE 会搞砸 QTableView(Qt、C++、sqlite)?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想问题出在 QSqlQueryModel::setQuery 你最终调用每次重新加载内容和插入\删除行时。看看 setQuery 实现,我建议:根据查询,您的模型可以重置,包括列设置更改,这应该触发视图列更新。
正如 Qt 文档所示:
所以我会使用直接 QSqlQuery 调用进行数据更新,然后重新加载模型使用相同的查询。或者考虑切换到 QSQLTableModel,这对于单个表内容操作非常方便并且支持插入更新和删除。看看下面的示例是否适合您:
设置数据库、视图和模型:
...
添加新行:
删除所选行:
提交更改:
希望这有帮助,问候
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:
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:
...
add new row:
delete selected row(s):
submit changes:
hope this helps, regards