using mutex when separate QSortFilterProxyModel and its source model in other thread?
-
Hi,
In my project, I move the source model object in second thread.
when I call sourceModel()->removeRows() funtion in Proxy model (main thread), will this function execute in main thread or second thread?.
Is it necessary to use mutex when delete and add data in source model? -
@NguyenMinh said in using mutex when separate QSortFilterProxyModel and its source model in other thread?:
I move the source model object in second thread.
That's not supported. Both models must stay in the GUI (main) thread to be accessible by the View.
Have a look at
QFileSystemModel
. The model stays in the main thread, but it creates other threads privately to gather data:- https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfilesystemmodel.h.html
- https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qfilesystemmodel_p.h.html
when I call sourceModel()->removeRows() funtion in Proxy model (main thread), will this function execute in main thread or second thread?
If you call it from the main thread, then it will execute in the main thread.
-
@JKSH I do something like this:
QApplication app(argc, argv); SongModel *model1=new SongModel; SortModel *sortModel=new SortModel(model1,SongModel::idRole); QThread thread2; model1->moveToThread(&thread2); thread2.start(); engine.rootContext()->setContextProperty("model1",model1); engine.rootContext()->setContextProperty("sortModel",sortModel);
my project run normal, show no error. What do you mean it's not supported?
-
@NguyenMinh said in using mutex when separate QSortFilterProxyModel and its source model in other thread?:
What do you mean it's not supported?
It may work but there is no guarantee and you will likely get strange errors.
-
@NguyenMinh said in using mutex when separate QSortFilterProxyModel and its source model in other thread?:
I do something like this:
QThread thread2; model1->moveToThread(&thread2); thread2.start();
Why? What do you want to do in that thread?
my project run normal, show no error. What do you mean it's not supported?
If the other thread calls a function that changes the model data (like
addRows()
orremoveRows()
) while you are sorting the GUI at the same time, your app can crash.