Results 1 to 4 of 4

Thread: Qt models and multi-threaded inserts

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2012
    Posts
    247
    Thanks
    29
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt models and multi-threaded inserts

    Hi,

    Say I have a model subclassed from QAbstractTableModel, which is shown in standard tablewidget.
    The model models a global datastore in my program, but this store is internally threadsafe, and it's assumed that multiple threads will modify the data in it, which can change the rowCount() in the model.

    I am now wondering how to do this safely. Sure, even in the ideal case, there might be a more or less short period where a view could display outdated data or empty rows or such.
    That's fine, but I want a guarantee it wont lead to corruption or crash.

    It's clear the begin/endResetModel methods in the model cant be called from another thread.
    But then what is the right way to do it?


    1) Even if i lock it somehow, is it sufficient to check the row/index in the data() method in order to avoid oob crashes? It should be, right, since the model abstracts my global store, and no other place accesses my global store.
    The danger is though that Qt internally implicitly assumes or asserts that all QModelIndex it ever encounters are always QModelIndex::row() < MyModel::rowCount(). I see no guarantee this is not the case.

    2) How to communicate the change to the backing store from thread X to the model/view living in the main thread Y.
    I suppose a queued signal-slot connection is the easiest way. That is, introduce a new thread_local notification class that lives on thread X, and connect two of its signals to the begin/endResetModel() slots of the model in thread Y.
    That way the change is communicated reasonably quickly, and because I check the size in data() (after locking the store), the worst thing that can happen is an empty row or outdated data in the view for a moment.
    Problem is that beingResetModel() and endResetModel() will potentially+probably both be processed *after* the modification is already made to the backing store. But that should be ok bc of the first question.

    Right?

    Thanks
    Last edited by tuli; 10th November 2024 at 18:30.

Similar Threads

  1. Multi-threaded rendering advice
    By jcox23 in forum Qt Programming
    Replies: 0
    Last Post: 5th November 2012, 11:25
  2. Debugging a multi threaded windows app
    By Cruz in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2012, 12:46
  3. signal not emitting from run() in multi-threaded app
    By naturalpsychic in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2011, 05:28
  4. Multi-threaded GUI possible?
    By nurtsi in forum Qt Programming
    Replies: 12
    Last Post: 26th November 2010, 21:52
  5. Qstring toStdString() and Multi-threaded DLL (/MD)
    By Daxos in forum Qt Programming
    Replies: 14
    Last Post: 15th May 2010, 11:57

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
  •  
Qt is a trademark of The Qt Company.