Results 1 to 1 of 1

Thread: interthread signals and slots to update model

  1. #1
    Join Date
    Feb 2010
    Posts
    14
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default interthread signals and slots to update model

    I have a custom item model: MyModel : public QAbstractItemModel
    that holds a std::vector<MyStruct> as internal data.
    I try to load a lot of items in that vector asynchronously, by using a worker thread.
    I have two solutions:

    A. - use the worker thread to load a std::vector<MyStruct>
    - pass the loaded data, though a custom event to the main thread
    - let the main thread update the model with the data copied inside the event

    B. - use the worker thread to load the std::vector<MyStruct>
    - connect a thread signal with loaded data as argument to a slot in the main thread (queued connection)
    - let the main thread, update the model with the data received as argument in the slot


    Both these solutions work, but they are updating the model inside the main thread.



    My question is: is there any way to update the model inside the worker thread? If yes, how?

    Let's say my model has a function

    Qt Code:
    1. void MyModel::setData(const std::vector<MyStruct> & data)
    2. {
    3. QMutexLocker locker(&m_mutex); //protect shared state, accessed from multiple threads
    4.  
    5. beginResetModel(); //when will this be sent/received?
    6.  
    7. ....update internal data
    8.  
    9. endResetModel(); //when will this be sent/received?
    10. }
    To copy to clipboard, switch view to plain text mode 

    and my worker thread does this:

    Qt Code:
    1. void MyThread::run()
    2. {
    3. std::vector<MyStruct> myVector;
    4. //load the data
    5. model->setData(myVector); //tricky: when will resetModel signals be emitted/received?
    6. }
    To copy to clipboard, switch view to plain text mode 

    What will happen if I call
    Qt Code:
    1. model->setData()
    To copy to clipboard, switch view to plain text mode 
    from the worker thread? When will the
    Qt Code:
    1. beginResetModel()
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. endResetModel()
    To copy to clipboard, switch view to plain text mode 
    be handled?

    I guess model will have to be mutexed, but I don't know if it will handle those beginResetModel() / endResetModel() properly.

    I keep reading http://doc.qt.nokia.com/latest/threads-qobject.html
    and I cannot figure this out.

    I guess resetModel signals will be either ignored OR handled later, when main thread's event loop will receive them (e.g views observing model will be notified too late)

    Thanks a lot,
    Chris
    Last edited by ctarsoaga; 2nd February 2011 at 12:19.

Similar Threads

  1. Replies: 1
    Last Post: 24th October 2010, 11:09
  2. Signals & Slots!
    By qtoptus in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2010, 01:50
  3. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 09:32
  4. Signals and slots
    By sylvarant in forum Newbie
    Replies: 4
    Last Post: 11th September 2007, 15:48
  5. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 08:11

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.