prasad_N
11th July 2015, 14:21
Hi,
I am using model view for displaying large data (tree view with millions of rows), I have customized my model & view (QAbstractItemModel, QTreeView).
What I have observed is as number of rows (data) grows high, updating of headers & selection becomes too slow.
I am changing the header data each time I click an item (if row is even number one header set, if odd another header set).
If my model has 1 - 1000000 rows, the header is updating as expected (no delay in updating header on clicking item).
I test by returning 10000000 rows(top level items) in model & when I started clicking on items header data is updating very slowly & items selection also becomes very slow.
When I tested with 20000000 rows it becomes further late.
Here is My implementation:
Model:
Model::Model(QObject *parent) :
QAbstractItemModel(parent)
{
}
Model::~Model()
{
}
void Model::setHeaderData(QStringList& head)
{
m_head = head; //m_header is QString List for headers
}
void Model::headerChanged(int row) //This is slot connected to view, will be called when an items clicked in view with index.row() as parameter.
{
QStringList head;
if(row%2){
head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " ;
}
else{
head << " Head11 " << " Head12 " << " Head13 " << " Head14 " << " Head15 " << " Head16 " << " Head17 " << " Head18 " ;
}
m_head = head;
emit headerDataChanged(Qt::Horizontal, 0, head.size() - 1);
}
QVariant Model::headerData ( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
return m_head.at(section);
}
else if ( orientation == Qt::Vertical && role == Qt::DisplayRole ){
return section;
}
return QVariant();
}
int Model::columnCount(const QModelIndex &parent) const
{
return 8;
}
int Model::rowCount(const QModelIndex &parent) const
{
if(!parent.isValid()) return 20000000;
if(parent.isValid() && !parent.parent().isValid()) return 2; // limit to two levels
return 0;
}
QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
{
if(parent.isValid())
{
// we only need id of the parent since it identifies the item
return createIndex(row, column, parent.row());
}
return createIndex(row, column, -1); // top level item has invalid parent
}
QModelIndex Model::parent(const QModelIndex &ind) const
{
if(!ind.isValid()) return QModelIndex();
int id = ind.internalId();
if( id == -1) return QModelIndex();
return index(id, 0); // return top-level item
}
QVariant Model::data(const QModelIndex &index, int role ) const
{
if(role == Qt::DisplayRole) return index.row()+1;
return QVariant();
}
Any Idea why updating headers is too slow when number of items becomes more ???
Thanks in advance.
I am using model view for displaying large data (tree view with millions of rows), I have customized my model & view (QAbstractItemModel, QTreeView).
What I have observed is as number of rows (data) grows high, updating of headers & selection becomes too slow.
I am changing the header data each time I click an item (if row is even number one header set, if odd another header set).
If my model has 1 - 1000000 rows, the header is updating as expected (no delay in updating header on clicking item).
I test by returning 10000000 rows(top level items) in model & when I started clicking on items header data is updating very slowly & items selection also becomes very slow.
When I tested with 20000000 rows it becomes further late.
Here is My implementation:
Model:
Model::Model(QObject *parent) :
QAbstractItemModel(parent)
{
}
Model::~Model()
{
}
void Model::setHeaderData(QStringList& head)
{
m_head = head; //m_header is QString List for headers
}
void Model::headerChanged(int row) //This is slot connected to view, will be called when an items clicked in view with index.row() as parameter.
{
QStringList head;
if(row%2){
head << " Head1 " << " Head2 " << " Head3 " << " Head4 " << " Head5 " << " Head6 " << " Head7 " << " Head8 " ;
}
else{
head << " Head11 " << " Head12 " << " Head13 " << " Head14 " << " Head15 " << " Head16 " << " Head17 " << " Head18 " ;
}
m_head = head;
emit headerDataChanged(Qt::Horizontal, 0, head.size() - 1);
}
QVariant Model::headerData ( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
return m_head.at(section);
}
else if ( orientation == Qt::Vertical && role == Qt::DisplayRole ){
return section;
}
return QVariant();
}
int Model::columnCount(const QModelIndex &parent) const
{
return 8;
}
int Model::rowCount(const QModelIndex &parent) const
{
if(!parent.isValid()) return 20000000;
if(parent.isValid() && !parent.parent().isValid()) return 2; // limit to two levels
return 0;
}
QModelIndex Model::index(int row, int column, const QModelIndex &parent) const
{
if(parent.isValid())
{
// we only need id of the parent since it identifies the item
return createIndex(row, column, parent.row());
}
return createIndex(row, column, -1); // top level item has invalid parent
}
QModelIndex Model::parent(const QModelIndex &ind) const
{
if(!ind.isValid()) return QModelIndex();
int id = ind.internalId();
if( id == -1) return QModelIndex();
return index(id, 0); // return top-level item
}
QVariant Model::data(const QModelIndex &index, int role ) const
{
if(role == Qt::DisplayRole) return index.row()+1;
return QVariant();
}
Any Idea why updating headers is too slow when number of items becomes more ???
Thanks in advance.