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:
// data model
{
public:
QList<SomeDataItem> getItems() const;
protected:
}
QList<SomeDataItem> MyDataModel::getItems() const
{
QList<SomeDataItem> items;
if (standardModel
->thread
() == QThread::currentThread()) {
// ... gather data in a usual way
}
else
{
// invoke it and wait until finished
QMetaObject::invokeMethod(this,
"getItems", Qt
::BlockingQueuedConnection, Q_RETURN_ARG
(QList<SomeDataItem>, items
));
}
return items;
}
// data model
class MyDataModel: public QObject
{
public:
QList<SomeDataItem> getItems() const;
protected:
QStandardItemModel standardModel;
}
QList<SomeDataItem> MyDataModel::getItems() const
{
QList<SomeDataItem> items;
if (standardModel->thread() == QThread::currentThread())
{
// ... gather data in a usual way
}
else
{
// invoke it and wait until finished
QMetaObject::invokeMethod(this, "getItems", Qt::BlockingQueuedConnection, Q_RETURN_ARG(QList<SomeDataItem>, items));
}
return items;
}
To copy to clipboard, switch view to plain text mode
// controller
Controller::run() // different thread
{
//
QList<SomeItem> items = model->getItems();
// ... do processing...
}
// controller
Controller::run() // different thread
{
//
QList<SomeItem> items = model->getItems();
// ... do processing...
}
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.
Bookmarks