Results 1 to 9 of 9

Thread: GUI becomes unresponsive when adding more items to TreeModel

  1. #1
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question GUI becomes unresponsive when adding more items to TreeModel

    Hi,

    I have a QTreeView with custom Read-only TreeModel derived from QAbstractItemModel. On click of a TreeItem, I have to call the backend layer to get the data and add the response to the parent TreeItem.

    Data coming from backend is divided into chunks(say 100 objects at a time which is pushed to the model, then 100 objects pushed to the model, so on). View is getting updated as and when model is getting updated by using BeginInsertRows()/EndInsertRows().

    My problem here is, When there are 10k data items coming in response which keeps on adding to model in chunks , the Treeview becomes unresponsive till the entire data gets added. I am not able to use scrollbar, click on any other TreeItem. Is there any way in Qt to make the data addition to the model/view without affecting other GUI controls?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    Adding chunks sounds already like a good way to avoid blocking, but maybe you are using a blocking loop until you have all chunks?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    The calls are in Asynchronous way. The backend layer push the data to the GUI layer and the GUI emits it every time to the respective widget. How the emit call works in Qt if it has queue of data to be pushed ? Is it blocking ?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    A single pair of beginInsertRows() and endInsertRows() with 100 rows should not be taking long.

    Have you measured the time for this?
    E.g. using QElapsedTimer.

    Something like
    Qt Code:
    1. QElapsedTimer timer;
    2. timer.start();
    3.  
    4. beginInserRows(....);
    5.  
    6. // update your data
    7.  
    8. endInsertRows();
    9.  
    10. qDebug() << "Update time for one chunk of data:" << timer.elapsed();
    To copy to clipboard, switch view to plain text mode 

    This is the time the UI will be "blocked" as this happens on the main thread the same way as processing user and paint events.

    Be sure that the method doing this is really being called asynchronously, i.e. execution returning to the event loop between calls.

    Cheers,
    _

  5. #5
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    Hi, a correction on the understanding. Its not 100 rows insertion at a time to the model.
    A single pair of beginInsertRows() and endInsertRows() with 1 row at a time. The entire insertion is happening in a loop of count 100.
    Is that the problem ?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,462
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    Of course.
    Qt Code:
    1. beginInsertRows();
    2. Loop....
    3. endInsertRows();
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,165
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    Is that the problem ?
    As Lesiok said, yes, of course. What you are doing is forcing the UI to update with -every- new row. Do as he suggests - call beginInsertRows() with a row count of 100, add the rows to your internal model, then call endInsertRows() to update the UI.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Jun 2018
    Location
    India
    Posts
    34
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    What will be the first and last index do I need to give in beginInsertRows() ,If I am trying to append 100 nodes to parent.

    Currently my code is

    beginInsertRows(parent, 0, 0);
    for ()
    {
    parentNode->AppendChild(child));
    }

    endInsertRows();

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,165
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: GUI becomes unresponsive when adding more items to TreeModel

    If I am trying to append 100 nodes to parent.
    Your code is incorrect. Read the documentation. QAbstractItemModel::beginInsertRows() explains it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QListWidget, help adding new items.
    By JeremyRussell in forum Newbie
    Replies: 4
    Last Post: 7th April 2011, 00:54
  2. Replies: 1
    Last Post: 4th February 2011, 02:41
  3. Adding new items to model (MVC)
    By Urvin in forum Qt Programming
    Replies: 1
    Last Post: 7th November 2010, 08:54
  4. QListView - adding items
    By creep33 in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2010, 18:30
  5. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 11:03

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.