Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calls from QML to context property objects only in main thread?
Forum Update on Monday, May 27th 2025

Calls from QML to context property objects only in main thread?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 705 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on 11 Feb 2021, 10:18 last edited by
    #1

    Is there some kind of guarantee that all calls from QML to context property object's Q_PROPERTY and Q_INVOKABLE entry points only happen in the main (GUI) thread?

    I was looking for documentation on this, but didn't find any.

    Question is: Do I need to implement my functions reentrant or thread-safe?

    K 1 Reply Last reply 11 Feb 2021, 10:39
    0
    • A Asperamanca
      11 Feb 2021, 10:18

      Is there some kind of guarantee that all calls from QML to context property object's Q_PROPERTY and Q_INVOKABLE entry points only happen in the main (GUI) thread?

      I was looking for documentation on this, but didn't find any.

      Question is: Do I need to implement my functions reentrant or thread-safe?

      K Offline
      K Offline
      KroMignon
      wrote on 11 Feb 2021, 10:39 last edited by KroMignon 2 Nov 2021, 10:45
      #2

      @Asperamanca said in Calls from QML to context property objects only in main thread?:

      Is there some kind of guarantee that all calls from QML to context property object's Q_PROPERTY and Q_INVOKABLE entry points only happen in the main (GUI) thread?

      I don't think so.
      Depending on backend used, it is possible that QML rendering uses multiple threads (cf. https://doc.qt.io/qt-5/qtquick-visualcanvas-scenegraph.html), so I suppose this will also be possible for calls from QML.

      If you are calling directly a function from QML (Q_INVOKABLE or slot) you should ensure it is thread safe, but Q_PROPERTY() should not be a problem, those will be called from event loop and therefore should be in the right thread.

      EDIT: after reading documentation, I change my mind ;)

      QML is executed by QQmlContext (https://doc.qt.io/qt-5/qqmlcontext.html), which runs in the thread in which it has been created, so if you create your QML context in main thread and also the C++ classes, it should not be required.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      A 1 Reply Last reply 11 Feb 2021, 10:47
      0
      • K KroMignon
        11 Feb 2021, 10:39

        @Asperamanca said in Calls from QML to context property objects only in main thread?:

        Is there some kind of guarantee that all calls from QML to context property object's Q_PROPERTY and Q_INVOKABLE entry points only happen in the main (GUI) thread?

        I don't think so.
        Depending on backend used, it is possible that QML rendering uses multiple threads (cf. https://doc.qt.io/qt-5/qtquick-visualcanvas-scenegraph.html), so I suppose this will also be possible for calls from QML.

        If you are calling directly a function from QML (Q_INVOKABLE or slot) you should ensure it is thread safe, but Q_PROPERTY() should not be a problem, those will be called from event loop and therefore should be in the right thread.

        EDIT: after reading documentation, I change my mind ;)

        QML is executed by QQmlContext (https://doc.qt.io/qt-5/qqmlcontext.html), which runs in the thread in which it has been created, so if you create your QML context in main thread and also the C++ classes, it should not be required.

        A Offline
        A Offline
        Asperamanca
        wrote on 11 Feb 2021, 10:47 last edited by Asperamanca 2 Nov 2021, 10:47
        #3

        @KroMignon
        Thank you. I thought pretty much the same...while the rendering can happen in multiple threads, the data acquisition should be in one thread only.

        EDIT: And for QQuickImageProviders they explicitly say you need to be able to handle calls from different threads.

        K 1 Reply Last reply 11 Feb 2021, 10:52
        0
        • A Asperamanca
          11 Feb 2021, 10:47

          @KroMignon
          Thank you. I thought pretty much the same...while the rendering can happen in multiple threads, the data acquisition should be in one thread only.

          EDIT: And for QQuickImageProviders they explicitly say you need to be able to handle calls from different threads.

          K Offline
          K Offline
          KroMignon
          wrote on 11 Feb 2021, 10:52 last edited by
          #4

          @Asperamanca said in Calls from QML to context property objects only in main thread?:

          EDIT: And for QQuickImageProviders they explicitly say you need to be able to handle calls from different threads.

          Yes, this is normal because renderer could be multiple-threaded.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          A 1 Reply Last reply 11 Feb 2021, 11:39
          0
          • K KroMignon
            11 Feb 2021, 10:52

            @Asperamanca said in Calls from QML to context property objects only in main thread?:

            EDIT: And for QQuickImageProviders they explicitly say you need to be able to handle calls from different threads.

            Yes, this is normal because renderer could be multiple-threaded.

            A Offline
            A Offline
            Asperamanca
            wrote on 11 Feb 2021, 11:39 last edited by
            #5

            @KroMignon said in Calls from QML to context property objects only in main thread?:

            @Asperamanca said in Calls from QML to context property objects only in main thread?:

            EDIT: And for QQuickImageProviders they explicitly say you need to be able to handle calls from different threads.

            Yes, this is normal because renderer could be multiple-threaded.

            My conclusion here is that they would note it elsewhere, if calls from multiple threads are to be expected.

            1 Reply Last reply
            0

            1/5

            11 Feb 2021, 10:18

            • Login

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