更新 QSqlTableModel 后视图未重绘

发布于 2024-11-01 22:46:50 字数 1209 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

記憶穿過時間隧道 2024-11-08 22:46:50

是的,我知道这是一个老问题,但我会为以后可能需要的人提供答案。

据我从代码中看到,QSqlQuery只会更新数据库而不是模型。

我将使用 QSqlTableModel 的功能:

bool TaskManager::addTask(Task &task)
{
    QSqlRecord record = this->record();
    record.setValue("description", task.description);
    record.setValue("numbers", task.numbers);
    record.setValue("imageid", task.imageid);

    // -1 means append
    return insertRecord(-1, record);
}

正如 文档所述

在位置行插入记录。如果 row 为负数,则记录将追加到末尾。内部调用 insertRows() 和 setRecord()。
如果可以插入记录则返回 true,否则返回 false。
OnFieldChange 和 OnRowChange 的更改会立即提交。失败不会在模型中留下新行。

这应该可以发挥更新数据库和模型的所有作用。

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:

bool TaskManager::addTask(Task &task)
{
    QSqlRecord record = this->record();
    record.setValue("description", task.description);
    record.setValue("numbers", task.numbers);
    record.setValue("imageid", task.imageid);

    // -1 means append
    return insertRecord(-1, record);
}

As the documentation says:

Inserts the record at position row. If row is negative, the record will be appended to the end. Calls insertRows() and setRecord() internally.
Returns true if the record could be inserted, otherwise false.
Changes are submitted immediately for OnFieldChange and OnRowChange. Failure does not leave a new row in the model.

This should do all the magic to update the database as well as the model.

北方。的韩爷 2024-11-08 22:46:50

根据 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.

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