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. QML with CPP model - updating model from worker thread
QtWS25 Last Chance

QML with CPP model - updating model from worker thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 448 Views
  • 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.
  • L Offline
    L Offline
    Lior
    wrote on last edited by
    #1

    hi
    I implemented a model-view example.
    model is CPP
    view is QML

    Currently, I can update the model from the view.

    I want to run a worker thread that receives data with UDP and updates the model.

    Can I trigger the model update slot from the thread ?

    For example - something like this

    void CMyModel::updateModel(int row)
    {
        bool change = false;
        beginInsertRows(QModelIndex(), row, row);
        //logic
        change = true;
        endInsertRows();
    
        if(change)
            emit dataChanged();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Use signals and slots between your thread and the model.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Use signals and slots between your thread and the model.

        L Offline
        L Offline
        Lior
        wrote on last edited by
        #3

        @SGaist
        I just want to be sure,
        In case my MODEL is interacting with the GUI (QML) (GUI can send updates to the model (CPP)).
        If I properly protected with locks, is it OK to update from another thread?

        " VIEW(QML) ---send update --- > MODEL(CPP) <---- send update --- UDP THREAD "

        in my case, the data that I receive from UDP and send to the view is a one-way flow
        but my question is to understand the general case

        hope I explained myself
        tanks

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The "send to the view" part is not clear. The schema looks like you are passing through the model but your writing makes it look like you try to access the GUI directly from the thread.

          So how exactly are you doing it ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply
          1
          • SGaistS SGaist

            The "send to the view" part is not clear. The schema looks like you are passing through the model but your writing makes it look like you try to access the GUI directly from the thread.

            So how exactly are you doing it ?

            L Offline
            L Offline
            Lior
            wrote on last edited by
            #5

            @SGaist
            hi,

            the correct flow
            " UDP THREAD --- updateModel ---> MODEL(CPP) ---- emit update ---> VIEW(QML) "

            The use case is
            get data ( UDP)
            process the data
            update the model
            emit the updated model changes to the GUI

            (I'm new to QML, and I do not exactly understand the property biding between CPP files to QML.)

            ** I will design the solution in more detail, the UDP thread is not going to do the processing,
            it's just for the question **

            for example - monitoring data
            VIEW - QML that shows tables, stacks, and graphs
            MODEL - the data after some processing.
            UDP thread - get monitored data

            Is it ok to update the model that binds to QML from the UDP thread?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Don't modify the model directly from the thread. Use signals and slots to transfer the data to the model.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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