<table><tr><td style="">jfita created this revision.<br />jfita added reviewers: staniek, piggz.<br />Herald added a project: KDb.<br />Herald added a subscriber: Kexi-Devel-list.<br />jfita requested review of this revision.
</td><a style="text-decoration: none; padding: 4px 8px; margin: 0 8px 8px; float: right; color: #464C5C; font-weight: bold; border-radius: 3px; background-color: #F7F7F9; background-image: linear-gradient(to bottom,#fff,#f1f0f1); display: inline-block; border: 1px solid rgba(71,87,120,.2);" href="https://phabricator.kde.org/D29166">View Revision</a></tr></table><br /><div><strong>REVISION SUMMARY</strong><div><p>It is a reimplementation of the same example from Qt: an application<br />
that demonstrates how to use a table view to show an edit a table with<br />
KDb, caching any changes to the data until the user explicitly submits<br />
them.  See <a href="https://doc.qt.io/qt-5/qtsql-cachedtable-example.html" class="remarkup-link" target="_blank" rel="noreferrer">https://doc.qt.io/qt-5/qtsql-cachedtable-example.html</a>.</p>

<p>According to Jarosław, the only currently available QTableView-like<br />
widget that has support for KDb is part of KEXI and can not be easily<br />
reused. Moreover, KDb does not provide models based on<br />
QAbstractItemModel.  Therefore the example either had to create a table<br />
widget or a proxy QAbstractItemModel to KDb’s cursor. I did the latter.</p>

<p>Qt’s example creates a SQLite database on memory (i.e., ":memory:" as<br />
database path), but KDb does not allow that: for file-based drivers, it<br />
checks that the driver actually created the file. Therefore i had to<br />
create a temporary database on disk that gets deleted at exit.</p>

<p>The table’s sample data is the same that is being used as an example<br />
data source for KReport.</p>

<p>As for the QAbstactTableModel implementation, at first i tried to use an<br />
instance of KDbTableViewData and, like KEXI does, preload all its<br />
records at the begining. This worked as expected.</p>

<p>However, i believe that i can not make use of KDbTableViewData’s edit<br />
buffer to hold the cached data because, as i understood it, that buffer<br />
can only hold the data for a single record —a row— while the example<br />
wants to cache all changes to all records. I guess that<br />
KDbTableViewData’s user case is for KEXI’s commit-on-row-change<br />
behavior.</p>

<p>I made it work by keeping a map of row→KDbRecordEditBuffer inside the<br />
model proxy, but at that point KDbTableViewData was of little benefit<br />
and i dropped it in favor of using the cursor instead.</p>

<p>Finally, i had a bit of trouble with cleaning up of cursor instances.<br />
The example’s TableModel is responsible for the cursor it creates and<br />
therefore must delete it in its destructor. At first i deleted the<br />
instance of TableModel inside the main window’s destructor, called when<br />
the application exits, but it seems that KDbDriverManager registers a<br />
slot against Qt’s aboutToQuit, a signal emitted *before* the main window<br />
is deleted. At that point TableModel was holding a dangling cursor and<br />
received a SIGSEGV.</p>

<p>Being an example i could, maybe, just ignore the deletion of that<br />
cursor, but then i was worried that it would seem like there is no need<br />
to close or delete cursors. That is why i now delete the table model<br />
when the main window is closed, before the aboutToQuit signal is<br />
emitted.</p></div></div><br /><div><strong>REPOSITORY</strong><div><div>R15 KDb</div></div></div><br /><div><strong>BRANCH</strong><div><div>cachedtable-example</div></div></div><br /><div><strong>REVISION DETAIL</strong><div><a href="https://phabricator.kde.org/D29166">https://phabricator.kde.org/D29166</a></div></div><br /><div><strong>AFFECTED FILES</strong><div><div>examples/CMakeLists.txt<br />
examples/cachedtable/CMakeLists.txt<br />
examples/cachedtable/MessageHandler.cpp<br />
examples/cachedtable/MessageHandler.h<br />
examples/cachedtable/TableEditor.cpp<br />
examples/cachedtable/TableEditor.h<br />
examples/cachedtable/TableEditor.ui<br />
examples/cachedtable/TableModel.cpp<br />
examples/cachedtable/TableModel.h<br />
examples/cachedtable/main.cpp</div></div></div><br /><div><strong>To: </strong>jfita, staniek, piggz<br /><strong>Cc: </strong>Kexi-Devel-list, barman, wicik, staniek<br /></div>