Results 1 to 5 of 5

Thread: [QStandardItemModel] read from a different thread

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default [QStandardItemModel] read from a different thread

    Hello. I use model-view-conroller pattern in my project. Controller is a different thread running in a background, sometimes it needs to loop through the data stored in model and make some processing, but as long as it is a different thread I don't know how to do this in a safe way. Could you, smart people, give me some advice?
    Last edited by mentalmushroom; 8th November 2011 at 09:04.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [QStandardItemModel] read from a different thread

    The better way is keep model in the GUI thread, and move only the long processing loop to different thread.
    View will take data from model, model if required will trigger the processing loop and continue, once the loop is finished it should inform the model using a signal, and model should update the view with new data.

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: [QStandardItemModel] read from a different thread

    Thanks for your reply, but my question was rather regarding controller. Model and view are in the same thread, but controller works in a different thread, where it needs to get some data from the model. I decided to create an additional class that wraps QStandardItemModel and added methods like to keep it thread safe:

    Qt Code:
    1. // data model
    2.  
    3. class MyDataModel: public QObject
    4. {
    5. public:
    6. QList<SomeDataItem> getItems() const;
    7.  
    8. protected:
    9. QStandardItemModel standardModel;
    10. }
    11.  
    12. QList<SomeDataItem> MyDataModel::getItems() const
    13. {
    14. QList<SomeDataItem> items;
    15. if (standardModel->thread() == QThread::currentThread())
    16. {
    17. // ... gather data in a usual way
    18. }
    19. else
    20. {
    21. // invoke it and wait until finished
    22. QMetaObject::invokeMethod(this, "getItems", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QList<SomeDataItem>, items));
    23. }
    24. return items;
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // controller
    2.  
    3. Controller::run() // different thread
    4. {
    5. //
    6. QList<SomeItem> items = model->getItems();
    7. // ... do processing...
    8. }
    To copy to clipboard, switch view to plain text mode 

    Only I am not sure it is the good way to do that. However, I didn't find any explanation regarding multithreaded model-view-controller.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [QStandardItemModel] read from a different thread

    Qt Code:
    1. // invoke it and wait until finished
    2. QMetaObject::invokeMethod(this, "getItems", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QList<SomeDataItem>, items));
    To copy to clipboard, switch view to plain text mode 
    Invoke and continue / return don't wait (that is purpose of multi-threading). When the thread is done let it emit a signal and wait for the model to read the data.

  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: [QStandardItemModel] read from a different thread

    hmmm... not sure what you meant "return don't wait". it should wait for return due to Qt::BlockingQueuedConnection, as far as i understood documentation. yes, it would be possible to emit signal from controller and continue until model emits reply signal, but it gets a bit complicated comparing to just call and wait, because controller can't continue without that data anyway.


    Added after 1 45 minutes:


    Ok, sorry, i think, i understood what you meant. QMetaObject::invokeMethod didn't work with Qt::BlockingQueuedConnection when i tried to return some result.
    Last edited by mentalmushroom; 10th November 2011 at 09:33.

Similar Threads

  1. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  2. Replies: 5
    Last Post: 22nd February 2011, 21:21
  3. Wait in thread till QTcp socket have some thing to read
    By hasnain in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2010, 12:46
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. qtableview QStandardItemModel read only
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2008, 16:42

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.