Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. using mutex when separate QSortFilterProxyModel and its source model in other thread?
Forum Updated to NodeBB v4.3 + New Features

using mutex when separate QSortFilterProxyModel and its source model in other thread?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 809 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NguyenMinh
    wrote on last edited by NguyenMinh
    #1

    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?

    JKSHJ 1 Reply Last reply
    0
    • N NguyenMinh

      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?

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      N 1 Reply Last reply
      4
      • JKSHJ JKSH

        @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.

        N Offline
        N Offline
        NguyenMinh
        wrote on last edited by
        #3

        @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?

        JKSHJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • N NguyenMinh

            @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?

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #5

            @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() or removeRows()) while you are sorting the GUI at the same time, your app can crash.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved