Skip to content

Commit c78c03b

Browse files
mgrojoMKleusberg
authored andcommitted
Avoid unnecessary queries when populating filtered table
When a table is filtered and the browsed tabled is changed and then we return to this filtered table, the table view shows several incorrect disabled rows. The same may happen when refreshing this table. This seems to be produced by a (yet unsolved) concurrency problem triggered by several consecutive queries, that are signalled by setting the text of the stored filters in the table header filter. These unnecessary queries are avoided by setting the stored filter values at the same time as the table in the SQL table model, making sure that no intermediate queries are executed while the filters are set. This seems to work around the concurrency problem and the symptom of the invalid rows does not appear in those situations.
1 parent dd4a71e commit c78c03b

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/MainWindow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,9 @@ void MainWindow::populateTable()
649649
}
650650
}
651651
if(only_defaults)
652-
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode);
652+
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, storedData.filterValues);
653653
else
654-
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, v);
654+
m_browseTableModel->setTable(tablename, storedData.sortOrderIndex, storedData.sortOrderMode, storedData.filterValues, v);
655655

656656
// There is information stored for this table, so extract it and apply it
657657
applyBrowseTableSettings(storedData);
@@ -708,9 +708,12 @@ void MainWindow::applyBrowseTableSettings(BrowseDataTableSettings storedData, bo
708708
// Filters
709709
if(!skipFilters)
710710
{
711+
// Set filters blocking signals, since the filter is already applied to the browse table model
711712
FilterTableHeader* filterHeader = qobject_cast<FilterTableHeader*>(ui->dataTable->horizontalHeader());
713+
bool oldState = filterHeader->blockSignals(true);
712714
for(auto filterIt=storedData.filterValues.constBegin();filterIt!=storedData.filterValues.constEnd();++filterIt)
713715
filterHeader->setFilter(filterIt.key(), filterIt.value());
716+
filterHeader->blockSignals(oldState);
714717
}
715718

716719
// Encoding

src/sqlitetablemodel.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void SqliteTableModel::setChunkSize(size_t chunksize)
116116
m_chunkSize = chunksize;
117117
}
118118

119-
void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortColumn, Qt::SortOrder sortOrder, const QVector<QString>& display_format)
119+
void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortColumn, Qt::SortOrder sortOrder, const QMap<int, QString> filterValues, const QVector<QString>& display_format)
120120
{
121121
// Unset all previous settings. When setting a table all information on the previously browsed data set is removed first.
122122
reset();
@@ -125,6 +125,9 @@ void SqliteTableModel::setTable(const sqlb::ObjectIdentifier& table, int sortCol
125125
m_sTable = table;
126126
m_vDisplayFormat = display_format;
127127

128+
for(auto filterIt=filterValues.constBegin(); filterIt!=filterValues.constEnd(); ++filterIt)
129+
updateFilter(filterIt.key(), filterIt.value(), false);
130+
128131
// The first column is the rowid column and therefore is always of type integer
129132
m_vDataTypes.push_back(SQLITE_INTEGER);
130133

@@ -704,7 +707,7 @@ QStringList SqliteTableModel::getColumns(std::shared_ptr<sqlite3> pDb, const QSt
704707
return listColumns;
705708
}
706709

707-
void SqliteTableModel::updateFilter(int column, const QString& value)
710+
void SqliteTableModel::updateFilter(int column, const QString& value, bool applyQuery)
708711
{
709712
// Check for any special comparison operators at the beginning of the value string. If there are none default to LIKE.
710713
QString op = "LIKE";
@@ -806,7 +809,8 @@ void SqliteTableModel::updateFilter(int column, const QString& value)
806809
}
807810

808811
// Build the new query
809-
buildQuery();
812+
if (applyQuery)
813+
buildQuery();
810814
}
811815

812816
void SqliteTableModel::clearCache()

src/sqlitetablemodel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SqliteTableModel : public QAbstractTableModel
8282
QString customQuery(bool withRowid);
8383

8484
/// configure for browsing specified table
85-
void setTable(const sqlb::ObjectIdentifier& table, int sortColumn = 0, Qt::SortOrder sortOrder = Qt::AscendingOrder, const QVector<QString> &display_format = QVector<QString>());
85+
void setTable(const sqlb::ObjectIdentifier& table, int sortColumn = 0, Qt::SortOrder sortOrder = Qt::AscendingOrder, const QMap<int, QString> filterValues = QMap<int, QString>(), const QVector<QString> &display_format = QVector<QString>());
8686

8787
void setChunkSize(size_t chunksize);
8888
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
@@ -110,7 +110,7 @@ class SqliteTableModel : public QAbstractTableModel
110110
static void removeCommentsFromQuery(QString& query);
111111

112112
public slots:
113-
void updateFilter(int column, const QString& value);
113+
void updateFilter(int column, const QString& value, bool applyQuery = true);
114114

115115
signals:
116116
void finishedFetch(int fetched_row_begin, int fetched_row_end);

0 commit comments

Comments
 (0)