I will clarify. The snippets are executed in two separate functions.
void CModel::OnNewQuote(const std::string& symbol, const CQuote& quote, uint fieldUpdateFlags)
{
ModelItemRowTable::const_iterator rowIter = m_modelItemRows.find(symbol.c_str());
if (m_modelItemRows.end() != rowIter)
{
int row = rowIter.value();
m_topUpdatedRow = (std::min)(m_topUpdatedRow, row);
m_bottomUpdatedRow = (std::max)(m_bottomUpdatedRow, row);
m_modelItems[row].data.Update(quote, fieldUpdateFlags);
}
else
{
emit beginInsertRows
(QModelIndex(), rowCount
(), rowCount
());
ModelItem item;
item.symbol = symbol.c_str();
item.data = quote;
m_modelItems.push_back(item);
m_modelItemRows[symbol.c_str()] = m_modelItems.count() - 1;
emit endInsertRows();
}
}
void CModel::OnNewQuote(const std::string& symbol, const CQuote& quote, uint fieldUpdateFlags)
{
ModelItemRowTable::const_iterator rowIter = m_modelItemRows.find(symbol.c_str());
if (m_modelItemRows.end() != rowIter)
{
int row = rowIter.value();
m_topUpdatedRow = (std::min)(m_topUpdatedRow, row);
m_bottomUpdatedRow = (std::max)(m_bottomUpdatedRow, row);
m_modelItems[row].data.Update(quote, fieldUpdateFlags);
}
else
{
emit beginInsertRows(QModelIndex(), rowCount(), rowCount());
ModelItem item;
item.symbol = symbol.c_str();
item.data = quote;
m_modelItems.push_back(item);
m_modelItemRows[symbol.c_str()] = m_modelItems.count() - 1;
emit endInsertRows();
}
}
To copy to clipboard, switch view to plain text mode
The above function is a slot connected to a signal that will be emitted by the working thread when new data is received (I use a queued connection here). In my test case the row insertion part takes place only when the model is created as this is the only time I am populating the table. After that I am only updating existing entries in my table. In the above code, m_topUpdatedRow and m_bottomUpdatedRow are used to keep track of the block containing all of the updated rows.
The model owns a QTimer that will time out at 1 second intervals. The timeout signal is connected to a slot that updates the table.
void OnTimeOut()
{
emit dataChanged(index(m_topUpdatedRow, 0), index(m_bottomUpdatedRow, columnCount()-1));
m_topUpdatedRow = (std::numeric_limits<int>::max)();
m_bottomUpdatedRow = (std::numeric_limits<int>::min)();
}
void OnTimeOut()
{
emit dataChanged(index(m_topUpdatedRow, 0), index(m_bottomUpdatedRow, columnCount()-1));
m_topUpdatedRow = (std::numeric_limits<int>::max)();
m_bottomUpdatedRow = (std::numeric_limits<int>::min)();
}
To copy to clipboard, switch view to plain text mode
The slot resets m_topUpdatedRow and m_bottomUpdatedRow to "degenerate" values to ensure that they will be set to sensible values the next time a new quote is received.
I am still observing the same problems and the table is still slow and unresponsive during updates.
Bookmarks