QListView in combination with a QSqlRelationTableModel - setData in relationModel - view is not updated
Unsolved
General and Desktop
-
Hello Forum,
I have a comprehension question about the QListView in combination with a QSqlRelationTableModel.
The following example works as well:
//mGermanFungusNameDataModel = QSqlRelationTableModel auto a = mGermanFungusNameDataModel->relationModel(2)->setData(mGermanFungusNameDataModel->relationModel(2)->index(relationRow, Column::Name), newGermanName, Qt::EditRole); auto b = mGermanFungusNameDataModel->setData(mGermanFungusNameDataModel->index(sourceRow, Column::RowId), id, Qt::EditRole); auto c = mGermanFungusNameDataModel->setData(mGermanFungusNameDataModel->index(sourceRow, Column::Name), fungusId, Qt::EditRole); auto d = mGermanFungusNameDataModel->setData(mGermanFungusNameDataModel->index(sourceRow, Column::GermanName), nameId, Qt::EditRole); qDebug() << "a:" << a << ", b:" << b << ", c:" << c << ", d:" << d; mGermanFungusNameDataModel->relationModel(2)->submitAll(); mGermanFungusNameDataModel->submitAll();
a-d return true. The data is also written to the database. But now if I omit submitAll() the QListView is not populated with the new data. I want to call the submitAll() later via a save button so that the data is written into the database. But the data in the QListView should already be updated so kind of "cache".
Do I need my own delegate for this or is there another way?
Thanks for tips and help
-
Hi,
How is your view setup ?
-
Very simple:
mGermanNameEditor = new ToggleListView(this); mGermanNameEditor->setModel(mGermanFungusNameDataModel); mGermanNameEditor->setModelColumn(Column::GermanName);
And ToggleListView is subclassed from QListView and this class looks like this:
togglelistview.h
#ifndef TOGGLELISTVIEW_H #define TOGGLELISTVIEW_H #include <QListView> #include <QMouseEvent> #include <QDebug> class ToggleListView : public QListView { Q_OBJECT public: explicit ToggleListView(QWidget *parent = nullptr); ~ToggleListView(); protected: void mousePressEvent(QMouseEvent *event) override; }; #endif // TOGGLELISTVIEW_H
togglelistview.cpp
#include "togglelistview.h" ToggleListView::ToggleListView(QWidget *parent): QListView(parent) {} ToggleListView::~ToggleListView() {} void ToggleListView::mousePressEvent(QMouseEvent *event) { Qt::KeyboardModifiers keys; keys.setFlag(Qt::ControlModifier); event->setModifiers(keys); QListView::mousePressEvent(event); }
-
Which submitAll does your code require ?