PDA

View Full Version : Model/View Performance



Stevildo
31st January 2015, 20:28
Hello
In my program I have two Buttons:
Button1 adds one item to my model
Button2 imports a file with x items to my model
Both buttons use the same method (appendRow) to append data to the model. The only difference is that for Button2 I use a "FOR-loop".
I use QStandardItemModel, QTreeview and QStandardItem

Problem:
If i use Button1 to add one item data is visible in a very short time.
If I use Button2 to import x=1000 items. The first import takes let me say 1 second. The second import of 1000 items takes 2 seconds. the third 3 seconds,...
Than if I use Button1 to add 1 item again it takes also 3 seconds!

Questions:
Why takes it the same time to add one or 1000 items? The time only depends on the number of data in the model.
What can I do to reduce the time for adding only one item?

Thank you for your help and sorry for my bad English!

PS: this is my very first Qt Project so please don't be too hard to me ; )

ChrisW67
31st January 2015, 20:33
Perhaps you can share the code you use to load the batches of rows.

Stevildo
31st January 2015, 20:58
Hey
In MyModel i have a list m_MSGList
If I add a item to the model I do the following:

m_MSGList.append(new MyMSGItem(this,...));


bool MyModel::importTrace(QString Filename){
bool ret=false;
QFile File(Filename);
QString CMD;
QByteArray WString, RString;
QRegExp WData("s([0-9a-fA-F]*)w([0-9a-fA-F]*).*");
QRegExp RData("\\*([0-9a-fA-F]*)r([0-9a-fA-F]*).*");
if(File.open(QIODevice::ReadOnly)){
while(!File.atEnd()){
CMD=File.readLine();
if(WData.indexIn(CMD) !=-1){
//qDebug()<<"W.Adr:"<<WData.cap(1)<<"W.Data:"<<WData.cap(2);
WString.append(WData.cap(1));
WString.append(WData.cap(2));
}
if(RData.indexIn(CMD) !=-1){
//qDebug()<<"R.Adr:"<<RData.cap(1)<<"R.Data:"<<RData.cap(2);
RString.append(RData.cap(1));
RString.append(RData.cap(2));
}
this->addMSG(QTime::currentTime(),WString,RString);
WString.clear();
RString.clear();

}
ret=true;
}
return ret;
}

MyMSGItem is a MyVirtualDataItem:

class MyMSGItem: public MyVirtualDataItem
{
explicit MyMSGItem(MyModel *root,...);
};

class MyVirtualDataItem
{
MyVirtualDataItem *m_Root;
MyRowItem *m_StdItem;
QList<MyVirtualDataItem *>m_Data;
};


class MyRowItem
{
public:
MyRowItem(QStandardItem* Parent);

QStandardItem *m_Tree;
QStandardItem *m_Value;
QStandardItem *m_Description;
QStandardItem *m_Timestamp;
QStandardItem *m_Type;
QStandardItem *m_WData;
QStandardItem *m_RData;
};

MyRowItem::MyRowItem(QStandardItem *Parent)
{
m_Tree=new QStandardItem();
m_Value=new QStandardItem();
m_Description=new QStandardItem();
m_Timestamp=new QStandardItem();
m_Type=new QStandardItem();
m_WData=new QStandardItem();
m_RData=new QStandardItem();
Parent->appendRow(QList<QStandardItem *>()<<m_Tree<<m_Value<<m_Description<<m_Timestamp<<m_Type<<m_WData<<m_RData);
}

wysota
1st February 2015, 07:52
You surely have data redundancy in your model. If you use QStandardItemModel then you don't have to keep own copies (or pointers) to the data as the item already contains everything. If instead you do want to have your own items then there is no point in using QStandardItemModel and you should implement your own model derived from QAbtractItemModel or one of the other classes Qt offers.

Stevildo
3rd February 2015, 20:37
Thank you wysota for your reply!
If I started with this project I read a lot about MVC and the Qt models. I thought that it would be very complex to implement my own model based on QAbtractItemModel.
And I wasn't sure if this will be really necessary. So for the first step I decided to use QStandardItemModel and expand it with my needed functionality. And everything works fine except the above written performance problem.
Do you think I can fix the problem by deriving from QAbstractItemModel?
Is there a good tutorial on how to do this? (I've found a link to a tutorial on this forum but I get an 404 Error...: http://doc.qt.io/qt-5/itemviews-simpletreemodel.html)
On which point do I have to ensure that I will not run to same trouble as I get with QStandardItemModel?
Is it only a problem of the model or also the view?
Is there no other QStandardItemModel based solution?

Thank you for your help!!

wysota
3rd February 2015, 20:50
Do you think I can fix the problem by deriving from QAbstractItemModel?
I cannot guarantee that but there is a good chance you will get decent performance boost if you implement the model properly.


Is there a good tutorial on how to do this? (I've found a link to a tutorial on this forum but I get an 404 Error...: http://doc.qt.io/qt-5/itemviews-simpletreemodel.html)
Following a tutorial will not do you much good. A good model has to be tailored to the data it holds. If you follow a tutorial your model will be suited for data presented in the tutorial. You have to first understand what you are doing.


Is it only a problem of the model or also the view?
You rarely have to touch the view. I'd focus on the model.


Is there no other QStandardItemModel based solution?
Without knowing the details of the behaviour of your data I cannot answer that question. Item based models are usually much slower compared to models which directly operate on real data structures and signal changes to the model in a smart way.