Results 1 to 6 of 6

Thread: Model/View Performance

  1. #1
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Model/View Performance

    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 ; )

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Model/View Performance

    Perhaps you can share the code you use to load the batches of rows.

  3. #3
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model/View Performance

    Hey
    In MyModel i have a list m_MSGList
    If I add a item to the model I do the following:
    Qt Code:
    1. m_MSGList.append(new MyMSGItem(this,...));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool MyModel::importTrace(QString Filename){
    2. bool ret=false;
    3. QFile File(Filename);
    4. QString CMD;
    5. QByteArray WString, RString;
    6. QRegExp WData("s([0-9a-fA-F]*)w([0-9a-fA-F]*).*");
    7. QRegExp RData("\\*([0-9a-fA-F]*)r([0-9a-fA-F]*).*");
    8. if(File.open(QIODevice::ReadOnly)){
    9. while(!File.atEnd()){
    10. CMD=File.readLine();
    11. if(WData.indexIn(CMD) !=-1){
    12. //qDebug()<<"W.Adr:"<<WData.cap(1)<<"W.Data:"<<WData.cap(2);
    13. WString.append(WData.cap(1));
    14. WString.append(WData.cap(2));
    15. }
    16. if(RData.indexIn(CMD) !=-1){
    17. //qDebug()<<"R.Adr:"<<RData.cap(1)<<"R.Data:"<<RData.cap(2);
    18. RString.append(RData.cap(1));
    19. RString.append(RData.cap(2));
    20. }
    21. this->addMSG(QTime::currentTime(),WString,RString);
    22. WString.clear();
    23. RString.clear();
    24.  
    25. }
    26. ret=true;
    27. }
    28. return ret;
    29. }
    To copy to clipboard, switch view to plain text mode 

    MyMSGItem is a MyVirtualDataItem:
    Qt Code:
    1. class MyMSGItem: public MyVirtualDataItem
    2. {
    3. explicit MyMSGItem(MyModel *root,...);
    4. };
    5.  
    6. class MyVirtualDataItem
    7. {
    8. MyVirtualDataItem *m_Root;
    9. MyRowItem *m_StdItem;
    10. QList<MyVirtualDataItem *>m_Data;
    11. };
    12.  
    13.  
    14. class MyRowItem
    15. {
    16. public:
    17. MyRowItem(QStandardItem* Parent);
    18.  
    19. QStandardItem *m_Tree;
    20. QStandardItem *m_Value;
    21. QStandardItem *m_Description;
    22. QStandardItem *m_Timestamp;
    23. QStandardItem *m_Type;
    24. QStandardItem *m_WData;
    25. QStandardItem *m_RData;
    26. };
    27.  
    28. MyRowItem::MyRowItem(QStandardItem *Parent)
    29. {
    30. m_Tree=new QStandardItem();
    31. m_Value=new QStandardItem();
    32. m_Description=new QStandardItem();
    33. m_Timestamp=new QStandardItem();
    34. m_Type=new QStandardItem();
    35. m_WData=new QStandardItem();
    36. m_RData=new QStandardItem();
    37. Parent->appendRow(QList<QStandardItem *>()<<m_Tree<<m_Value<<m_Description<<m_Timestamp<<m_Type<<m_WData<<m_RData);
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Stevildo; 31st January 2015 at 22:03.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View Performance

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Model/View Performance

    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!!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Model/View Performance

    Quote Originally Posted by Stevildo View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 24
    Last Post: 18th September 2013, 07:35
  2. Replies: 4
    Last Post: 18th April 2012, 19:11
  3. Replies: 0
    Last Post: 21st April 2010, 13:23
  4. Replies: 1
    Last Post: 1st February 2010, 19:42
  5. QSql Model-View: How to keep view in sync with model
    By schall_l in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 00:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.