Results 1 to 10 of 10

Thread: calling QCoreApplication::processEvents(); wenn filling a model with data

  1. #1
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default calling QCoreApplication::processEvents(); wenn filling a model with data

    Hi All,

    I am filling a QStandardItemModel with data, in fact 5000 row with 8 columns. At the same time as the model is being filled I am playing a mp3 file via the QMultimedia player and updating a QSlider with the duration.

    If I call the function below my main gui thread freezes up and becomes unrepsonsive.

    Qt Code:
    1. void MainWindow::addModl(QList<QStandardItem*> &data)
    2. {
    3. plmodel->appendRow(data);
    4. }
    To copy to clipboard, switch view to plain text mode 

    by calling the processEvents() the problem of the freezing main thread is solved but I have noticed that the QSlider is not updated every 1 seconds, but from to time the update happends with 2-3 seconds delay.



    Qt Code:
    1. void MainWindow::addModl(QList<QStandardItem*> &data)
    2. {
    3. QCoreApplication::processEvents();
    4. plmodel->appendRow(data);
    5. }
    To copy to clipboard, switch view to plain text mode 




    So my question is, is calling the processEvents() the correct approach to prevent the main gui from freezing up?
    -and are the gaps I see in the QSLider to be expected.

    In my case it is not vital that QSlider is updated every second, but I am thinking there could be other scenarios were you need all widgets to be updated in ms, in that case filling the model the way I am doing is not fine.


    What do you guys do when you have to do some heavy operation like this?

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

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    Do you have only transient access to the data or is the data also available later on?

    I.e. do you have to copy it into a standard item model before it is gone or does the application have access to the data at all times?

    In the second case it is likely a lot better to just create a model that interfaces with the actual data, instead of duplicating the data.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    In fact I am reading TAGs from mp3 files storing them in a DB (if not already stored), and then populating the model with the data from the DB.

    The problem is that when populating the model the other widgets are not updated as fast as I would like them too. At least not when I use the code I posted below.


    Could I write a model the interfaces the actual data in this case? I think not.

    thanks for your time!

    cheers,

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

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    Any reason you can't use any of the database models?

    Cheers,
    _

  5. #5
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    I could use a database model in this case. Would populating such a model be any different in terms of main thread blocking?

    I will give it a go, and let you know if those models are any better.

    Thanks again....

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    Well, I am only guessing, but your current setup sounds like you are adding one row after another, most likely while the model is already shown in a view.
    Each add results in updates on the view, causes memory to be copied, items to be created, etc.

    A model that just interfaces with the data would just report the total number of rows to the view. The view would then get all data it currently needs.
    No need for copying large amounts of data or even accessing data currently not displayed.

    Cheers,
    _

  7. #7
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    You are correct in your assumption. I do add the rows one by one, and the view is updated the whole time.

    but in this case I guess I wouldn't be able to do it any other way?. I am talking about a user adding 5000 or more titles to a play list. The model doesnt know yet what files the user will add so the data will have to be copied. - at least the first time they are added. the next time it could be retrieved from a database.

    How does other tools such as Itunes, banshee etc. deal with this kind of problem. I believe they will retrieve the data from the files, but not update the view until all rows are retrieved.

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

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    Even if the user adds files one by one (not likely), in the case of a model that is merely an interface to the data, it doesn't have to copy the data that is currently not needed by the view.

    In the more likely case that a users selects lists of files, e.g. whole directories or even directory trees, a model can expose all new rows in one go, or in chunks, etc.

    Cheers,
    _

  9. #9
    Join Date
    Jun 2012
    Posts
    38
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    In the more likely case that a users selects lists of files, e.g. whole directories or even directory trees, a model can expose all new rows in one go, or in chunks, etc.
    I might have a wrong idea here. Please be patient with me :-)

    so you idea is to just add chunks of let say 100 rows at the time?

    is there away to do that? wont I still have to add the rows one by one to the model?

    -Remember it is likely the model has data already, I dont want to loose this data.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: calling QCoreApplication::processEvents(); wenn filling a model with data

    Exposing more rows in chunks is one option, e.g. if doing all the data in one go would leave the UI empty for too long.

    A model can expose any number of rows anywhere in its range at any time, see QAbstractItemModel::beginInsertRows().

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 23rd June 2014, 13:37
  2. Replies: 0
    Last Post: 14th April 2011, 15:10
  3. QTreeView + own model + dynamic filling
    By Urvin in forum Newbie
    Replies: 7
    Last Post: 19th February 2010, 17:11
  4. Replies: 4
    Last Post: 20th February 2007, 12:35

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.