更新 QSqlTableModel 后视图未重绘
QSqlTableModel 有一个子类。
class TaskManager : public QSqlTableModel
{
Q_OBJECT
public:
explicit TaskManager(QObject *parent = 0){}
void initMode();
bool addTask(Task &task);
...
}
这是 initModel()
void TaskManager::initModel()
{
setTable(currenttasks);
setEditStrategy(QSqlTableModel::OnFieldChange);
select();
}
这是 addTask 方法
bool TaskManager::addTask(Task &task)
{
QSqlQuery query;
query.prepare("INSERT INTO currenttasks (description, numbers, imageid) "
"VALUES (:descr, :numbers, :imageid)");
query.bindValue(":descr", task.description);
query.bindValue(":numbers", task.numbers);
query.bindValue(":imageid", task.imageid);
bool res = query.exec();
reset();
return res;
}
另外还有一个关于 QML 的视图。但是,如果我单击“ADD”按钮调用 addTask() 方法,我看不到结果。视图未重新绘制。还有一个从 QAbstractProxyModel 继承的代理 QMLifyProxyModel 但我不相信这就是问题所在。 当我重新启动应用程序时,我可以看到数据库中的更改。 模型更新后似乎没有读取数据。
顺便说一句,我尝试在查询后发出layoutChanged(),但没有结果。 我需要做什么才能立即在视图中获得新记录? 谢谢。
There is a sub-class of QSqlTableModel.
class TaskManager : public QSqlTableModel
{
Q_OBJECT
public:
explicit TaskManager(QObject *parent = 0){}
void initMode();
bool addTask(Task &task);
...
}
This is initModel()
void TaskManager::initModel()
{
setTable(currenttasks);
setEditStrategy(QSqlTableModel::OnFieldChange);
select();
}
And this is addTask method
bool TaskManager::addTask(Task &task)
{
QSqlQuery query;
query.prepare("INSERT INTO currenttasks (description, numbers, imageid) "
"VALUES (:descr, :numbers, :imageid)");
query.bindValue(":descr", task.description);
query.bindValue(":numbers", task.numbers);
query.bindValue(":imageid", task.imageid);
bool res = query.exec();
reset();
return res;
}
Also there's a view on QML. But if i click on "ADD" button calling addTask() method i cannot see the results. The view isn't redrawn. Also there is a proxy QMLifyProxyModel inherited from QAbstractProxyModel but i do not believe that this is the problem.
I can see the changes in DB when i restart the application.
It seems that the model does not read datas after updating.
By the way i tried to emit layoutChanged() after my query, no results.
What do i need to do that i immediately get new records in the view ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我知道这是一个老问题,但我会为以后可能需要的人提供答案。
据我从代码中看到,QSqlQuery只会更新数据库而不是模型。
我将使用 QSqlTableModel 的功能:
正如 文档所述:
这应该可以发挥更新数据库和模型的所有作用。
Yes, I know this is an old question, but I will give an answer for any that might need it later.
As far as I can see from the code the QSqlQuery will only update the database and not the model.
I would use the capabilities of QSqlTableModel:
As the documentation says:
This should do all the magic to update the database as well as the model.
根据 QMLifyProxyModel 的文档,它表示尚不支持根据源模型的更改更新模型。
因此,如果您使用 QMLifyProxyModel 从 QSqlTableModel 转换到 QML ListView,这可能就是您的视图未刷新的原因。
According to the documentation from QMLifyProxyModel it says that it does not yet support the update of the model upon changes to the source model.
So, if you use the QMLifyProxyModel to transition from the QSqlTableModel to the QML ListView than probably this is the reason why you're view is not refreshed.