This is my method for adding one item( a row ) to the tableview:
void AkViewItemModel::addItem(AkViewWidgetItem *newItem)
{
if(!newItem)
return;
int position = mItemsList.count();
newItem->mViewWidget = mHeaderItem->akViewWidget();
mItemsList.append(newItem);
endInsertRows();
}
void AkViewItemModel::addItem(AkViewWidgetItem *newItem)
{
if(!newItem)
return;
int position = mItemsList.count();
beginInsertRows(QModelIndex(), position, position);
newItem->mViewWidget = mHeaderItem->akViewWidget();
mItemsList.append(newItem);
endInsertRows();
}
To copy to clipboard, switch view to plain text mode
And below is the method to add a list of items:
void AkViewItemModel::addItems(QList<AkViewWidgetItem *> items)
{
int beginRow = mItemsList.count();
int numItems = items.count();
beginInsertRows
(QModelIndex(), beginRow, beginRow
+numItems
-1);
AkViewWidgetItem *item = NULL;
for(int i = 0; i < numItems; i++)
{
item = items[i];
item->mViewWidget = mHeaderItem->akViewWidget();
mItemsList.append(item);
}
endInsertRows();
}
void AkViewItemModel::addItems(QList<AkViewWidgetItem *> items)
{
int beginRow = mItemsList.count();
int numItems = items.count();
beginInsertRows(QModelIndex(), beginRow, beginRow+numItems-1);
AkViewWidgetItem *item = NULL;
for(int i = 0; i < numItems; i++)
{
item = items[i];
item->mViewWidget = mHeaderItem->akViewWidget();
mItemsList.append(item);
}
endInsertRows();
}
To copy to clipboard, switch view to plain text mode
When I add a lot of single items, it is really slow! But if I accumulate some items in my application and then add them through a list, it becomes a lot faster and solves my problem.
Why are these methods beginInsertRows and endInsertRows so slow?
Bookmarks