Results 1 to 8 of 8

Thread: Q_PROPERTY WRITE with slot between Threads

  1. #1
    Join Date
    Aug 2012
    Posts
    5

    Default Q_PROPERTY WRITE with slot between Threads

    Hello,

    I'm having issues with Qt's Property System.

    Basically i am trying to write a Q_PROPERTY of a Object that lives in a different thread. I'm doing this from QML. I was thinking, that if i declare the WRITE function as a slot, that function will be invoked in the Event Loop of the other Thread. Sadly this is not happening.

    I made i minimal Example for Windows which shows the Problem.

    How can i make the WRITE accessor function being invoked in the event loop? I'm using QT 4.7.4
    Attached Files Attached Files
    Last edited by EMCEE; 15th August 2012 at 12:12.

  2. #2
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanks
    10
    Thanked 27 Times in 22 Posts

    Default Re: Q_PROPERTY WRITE with slot between Threads

    QML seems to use Qt:irectConnection only. If this is an issue, you can go a little hacky and create custom signal in QML object, and then connect to it from C++ specifying Qt::QueuedConnection.
    Oleg Shparber

  3. #3
    Join Date
    Aug 2012
    Posts
    5

    Default Re: Q_PROPERTY WRITE with slot between Threads

    Quote Originally Posted by Oleg View Post
    QML seems to use Qt:irectConnection only. If this is an issue, you can go a little hacky and create custom signal in QML object, and then connect to it from C++ specifying Qt::QueuedConnection.
    Ok this is a possibility. I updated the example. Now i'm using a Signal as the WRITE accessor function, which calls the real slot.

    But i really dont like the idea of having to declare such Signals all over my Project.

    Will this be changed in future Qt versions?
    Attached Files Attached Files
    Last edited by EMCEE; 15th August 2012 at 15:13.

  4. #4
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanks
    10
    Thanked 27 Times in 22 Posts

    Default Re: Q_PROPERTY WRITE with slot between Threads

    File an issue on Qt bug tracker and explain your use-case. I'm not sure if QML should be aware of signal receiver thread.
    Oleg Shparber

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Q_PROPERTY WRITE with slot between Threads

    Qt Code:
    1. class MyQobjectClass : public QObject {
    2. Q_OBJECT
    3. Q_PROPERTY(int someValue READ getValue WRITE setValueInvoker NOTIFY valueChanged) //using a signal as a setter didn't you mean to use setValue()?
    4.  
    5. public:
    6. explicit MyQobjectClass(QObject *parent = 0);
    7.  
    8. public slots:
    9. void setValue(int newValue);
    10. int getValue();
    11.  
    12. signals:
    13. void setValueInvoker(int); //<<--- signal
    14. void valueChanged();
    15.  
    16. private:
    17. int value;
    To copy to clipboard, switch view to plain text mode 

    You use a signal as a setter.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Aug 2012
    Posts
    5

    Default Re: Q_PROPERTY WRITE with slot between Threads

    Quote Originally Posted by high_flyer View Post
    Qt Code:
    1. class MyQobjectClass : public QObject {
    2. Q_OBJECT
    3. Q_PROPERTY(int someValue READ getValue WRITE setValueInvoker NOTIFY valueChanged) //using a signal as a setter didn't you mean to use setValue()?
    4.  
    5. public:
    6. explicit MyQobjectClass(QObject *parent = 0);
    7.  
    8. public slots:
    9. void setValue(int newValue);
    10. int getValue();
    11.  
    12. signals:
    13. void setValueInvoker(int); //<<--- signal
    14. void valueChanged();
    15.  
    16. private:
    17. int value;
    To copy to clipboard, switch view to plain text mode 

    You use a signal as a setter.

    Exactly. And it is connected to the setValue(int) slot. So the slot is invoked in a the other thread.

    Look at the attachment of the first post!


    Added after 4 minutes:


    Quote Originally Posted by Oleg View Post
    File an issue on Qt bug tracker and explain your use-case. I'm not sure if QML should be aware of signal receiver thread.
    Yes, seems like it isnt aware of the receiver thread. Direct Connection is also used, when i call a slot or Q_INVOKABLE directly from QML.
    Last edited by EMCEE; 15th August 2012 at 16:36.

  7. #7
    Join Date
    Jun 2012
    Posts
    98
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    11

    Default Re: Q_PROPERTY WRITE with slot between Threads

    Depending on how it's designed you could use static data, which should be shared between threads (So to speak.)

    Much cleaner and faster.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Q_PROPERTY WRITE with slot between Threads

    Exactly. And it is connected to the setValue(int) slot. So the slot is invoked in a the other thread.
    My point was, that I am not sure if the setter called by Q_PROPERTY actually will *emit* the signal, rather then juts call it as a call back (function pointer) - and since a signal has no definition, it just probably will do nothing.
    But if you actually do get the signal invoked at all (no matter in what thread) then ok.

    To solve your problem however you can have a regular setter invoke your signal as Qt::QueuedConnection.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 3
    Last Post: 3rd July 2012, 12:48
  2. Replies: 8
    Last Post: 25th June 2011, 11:14
  3. Q_PROPERTY WRITE function, also a slot?
    By Eos Pengwern in forum Newbie
    Replies: 2
    Last Post: 12th December 2009, 22:29
  4. signal and slot across threads having event loop
    By travis in forum Qt Programming
    Replies: 6
    Last Post: 6th November 2007, 00:56
  5. Signal / Slot with specific argument and threads
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 15th September 2007, 00:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.