Results 1 to 5 of 5

Thread: [QStandardItemModel] read from a different thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 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.

  3. #3
    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.

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