We don't use QAbstractTableModel's rows and columns. Instead of this we use own containers and bind View with the dataChanged signal.
{
if (model) {
//connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SIGNAL(sizeChanged()));
connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(updatePropertiesForSaving()));
connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(updateTitle()));
connect(model, SIGNAL(resetSelectDataSet()), SLOT(onSelectDataSetReset()));
connect(model, SIGNAL(resetGlassDataSet()), SLOT(onGlassDataSetReset()));
showEarnColumns(isEarnColumnsShown_);
updatePropertiesForSaving();
resizeColumnsToContents();
}
}
void GlassView::setModel(QAbstractItemModel* model)
{
if (model) {
QTableView::setModel(model);
//connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SIGNAL(sizeChanged()));
connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(updatePropertiesForSaving()));
connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(updateTitle()));
connect(model, SIGNAL(resetSelectDataSet()), SLOT(onSelectDataSetReset()));
connect(model, SIGNAL(resetGlassDataSet()), SLOT(onGlassDataSetReset()));
showEarnColumns(isEarnColumnsShown_);
updatePropertiesForSaving();
resizeColumnsToContents();
}
}
To copy to clipboard, switch view to plain text mode
As soon as data has been received we notify View by
emit dataChanged();
emit dataChanged();
To copy to clipboard, switch view to plain text mode
Model's data method provides access to data
{
int currentDepth = ((userDepth_ > 0) && (userDepth_ < maximumDepth_) ? userDepth_ : maximumDepth_);
int offset = currentDepth - ((sortOrder_ == SortOrder::Ascending) ? agregatedAskQuantity_.count() : agregatedBidQuantity_.count());
offset = ((offset < 0 || !showEmptyLines_) ? 0 : offset);
int currentRow = index.row();
int currentColumn = index.column();
switch(role) {
case Qt::UserRole: { // data needed for emiting; not rounded prices only
if (currentColumn == 2 && (currentRow >= offset && currentRow < (keys_.count() + offset)))
return keys_[currentRow - offset];
if (index.row() < dataTableForDisplay_.count() && index.column() < dataTableForDisplay_[index.row()].count())
return dataTableForDisplay_[index.row()][index.column()];
return emptyVariant_;
}
case Qt::DisplayRole: {
// Draw last line (row)
if (index.row()+1 == rowCount_) {
switch(index.column()) {
case 1: {
QVariant total
= (columnOrder_
== ColumnOrder
::BuyPriceSell ? buyTotal_
: sellTotal_
);
return QString("%L1").
arg(total.
toDouble(),
0,
'f',
0);
}
case 2:
case 3: {
QVariant total
= (columnOrder_
== ColumnOrder
::BuyPriceSell ? sellTotal_
: buyTotal_
);
return QString("%L1").
arg(total.
toDouble(),
0,
'f',
0);
}
default:
return emptyVariant_;
}
}
// Draw data
return dataTableForDisplay_[index.row()][index.column()];
}; break;
case Qt::ToolTipRole:
return emptyVariant_;
case Qt::TextAlignmentRole:
if (index.row()+1 == rowCount_ && index.column() == 2)
return QVariant(Qt
::AlignHCenter|Qt
::AlignVCenter);
// for return QVariant(Qt
::AlignRight|Qt
::AlignVCenter);
case Qt::FontRole: {
if((index.row() + 1) == rowCount_ && (index.column() == 1 || index.column() == 3)){
return variantBoldFont_;
}
return emptyVariant_;
}
case Qt::BackgroundRole: //Qt::BackgroundColorRole
if (index.row() % 2)
return QBrush(modelSettings_.
evenRowColor);
return QBrush(modelSettings_.
oddRowColor);
case Qt::ForegroundRole: {//Qt::TextColorRole
// if (currentRow >= offset && currentRow < (keys_.count() + offset)) {
// Price price = keys_[currentRow - offset];
// if (orderPricesBuy_.contains(price) || orderPricesSell_.contains(price))
// return QBrush(QColor(250, 20, 20)); // indicate our bid
// }
return QBrush(modelSettings_.
normalTextColor);
}
case Qt::WhatsThisRole:
return emptyVariant_;
default:
return emptyVariant_;
}
}
QVariant GlassModel::data( const QModelIndex &index, int role ) const
{
int currentDepth = ((userDepth_ > 0) && (userDepth_ < maximumDepth_) ? userDepth_ : maximumDepth_);
int offset = currentDepth - ((sortOrder_ == SortOrder::Ascending) ? agregatedAskQuantity_.count() : agregatedBidQuantity_.count());
offset = ((offset < 0 || !showEmptyLines_) ? 0 : offset);
int currentRow = index.row();
int currentColumn = index.column();
switch(role) {
case Qt::UserRole: { // data needed for emiting; not rounded prices only
if (currentColumn == 2 && (currentRow >= offset && currentRow < (keys_.count() + offset)))
return keys_[currentRow - offset];
if (index.row() < dataTableForDisplay_.count() && index.column() < dataTableForDisplay_[index.row()].count())
return dataTableForDisplay_[index.row()][index.column()];
return emptyVariant_;
}
case Qt::DisplayRole: {
// Draw last line (row)
if (index.row()+1 == rowCount_) {
switch(index.column()) {
case 1: {
QVariant total = (columnOrder_ == ColumnOrder::BuyPriceSell ? buyTotal_ : sellTotal_);
return QString("%L1").arg(total.toDouble(), 0, 'f', 0);
}
case 2:
return QString(tr("Р'С_РчР_Р_"));
case 3: {
QVariant total = (columnOrder_ == ColumnOrder::BuyPriceSell ? sellTotal_ : buyTotal_);
return QString("%L1").arg(total.toDouble(), 0, 'f', 0);
}
default:
return emptyVariant_;
}
}
// Draw data
return dataTableForDisplay_[index.row()][index.column()];
}; break;
case Qt::ToolTipRole:
return emptyVariant_;
case Qt::TextAlignmentRole:
if (index.row()+1 == rowCount_ && index.column() == 2)
return QVariant(Qt::AlignHCenter|Qt::AlignVCenter); // for
return QVariant(Qt::AlignRight|Qt::AlignVCenter);
case Qt::FontRole: {
if((index.row() + 1) == rowCount_ && (index.column() == 1 || index.column() == 3)){
return variantBoldFont_;
}
return emptyVariant_;
}
case Qt::BackgroundRole: //Qt::BackgroundColorRole
if (index.row() % 2)
return QBrush(modelSettings_.evenRowColor);
return QBrush(modelSettings_.oddRowColor);
case Qt::ForegroundRole: {//Qt::TextColorRole
// if (currentRow >= offset && currentRow < (keys_.count() + offset)) {
// Price price = keys_[currentRow - offset];
// if (orderPricesBuy_.contains(price) || orderPricesSell_.contains(price))
// return QBrush(QColor(250, 20, 20)); // indicate our bid
// }
return QBrush(modelSettings_.normalTextColor);
}
case Qt::WhatsThisRole:
return emptyVariant_;
default:
return emptyVariant_;
}
}
To copy to clipboard, switch view to plain text mode
dataTableForDisplay_ already contains prepared for displaying data.
So we are looking for any significant performance related improvents of this code. Let me know if such approach isn't applicable for fast data displaying and point me to the right direction if it's possible. Thank you.
PS
Sorry for my English
Bookmarks