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. Updating bindable property from another thread
Forum Updated to NodeBB v4.3 + New Features

Updating bindable property from another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 123 Views 2 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.
  • T Offline
    T Offline
    TheBill16
    wrote last edited by TheBill16
    #1

    According to the documentation:

    Bindable properties are not threadsafe, unless stated otherwise. A bindable property must not be read or modified by any thread other than the one is was created in.

    So if one needed to update the property in another thread, is the following approach valid?

    class Foo : public QObject {
        Q_OBJECT
        Q_OBJECT_BINDABLE_PROPERTY(Foo, Bar, m_bar)
    public Q_SLOTS:
        void updateBar() {
            // Run in another thread
            QThreadPool::globalInstance()->start([this]{
                // Fetch newBar...
                metaObject(this, [this, newBar] {
                    m_bar = newBar;
                }, Qt::QueuedConnection);
            });
        }
    }
    

    I have not faced any warning with this approach but just want to be sure.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      IgKh
      wrote last edited by IgKh
      #2

      It is very wasteful... why spawn a potentially new thread whose only job is schedule something on a completely different thread and immediately end?

      void updateBar(Bar newBar) {
          QMetaObject::invokeMethod(this, [this, newBar] { m_bar = newBar });
      }
      

      Should work just as well. Note that this example sticks to the default connection strategy - this way if you call updaBar from the same thread as this it will execute directly without overhead, and if you call it from another thread it will schedule the lambda for execution on the correct thread's event loop.

      T 1 Reply Last reply
      0
      • I IgKh

        It is very wasteful... why spawn a potentially new thread whose only job is schedule something on a completely different thread and immediately end?

        void updateBar(Bar newBar) {
            QMetaObject::invokeMethod(this, [this, newBar] { m_bar = newBar });
        }
        

        Should work just as well. Note that this example sticks to the default connection strategy - this way if you call updaBar from the same thread as this it will execute directly without overhead, and if you call it from another thread it will schedule the lambda for execution on the correct thread's event loop.

        T Offline
        T Offline
        TheBill16
        wrote last edited by
        #3

        @IgKh Thanks! Good to know that this will work.

        It is very wasteful... why spawn a potentially new thread whose only job is schedule something on a completely different thread and immediately end?

        It's just a simplify example. In my app, data need to be fetched from a 3rd party library in another thread to not lock up the main one, and updateBar would be much more complicated in actuality.

        Note that this example sticks to the default connection strategy - this way if you call updaBar from the same thread as this it will execute directly without overhead, and if you call it from another thread it will schedule the lambda for execution on the correct thread's event loop.

        Good point! Though, when I was writing this, I only considered the need to be called in another thread, so I must have been a bit tunnel vision.

        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