Results 1 to 11 of 11

Thread: invoke methods in another thread and get return values

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: invoke methods in another thread and get return values

    Just to be clear I understand:
    Are you looking for signals/slots with return values other than void, is that it?
    ==========================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.

  2. #2
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: invoke methods in another thread and get return values

    Yes, it's the slots returning values.

  3. #3
    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: invoke methods in another thread and get return values

    I can't test at the moment, but its possible that slots actually can be defined with a return value - however, when used as a slot, how do you want to get the value?
    Since you are not calling the slot directly, but only invoke its call (as in emit a signal), and not by the object invoking emitting the signal - so what good would it be for you if the slot is retuning a value - you can't get to that value any way (in the object that emitted the signal)!
    Or in other words, you can get the return value only there where the slot is really called - namely in the MetaObject but there you probably wont know what to do with the return value...

    Try defining a slot that returns a value and see if the connection works.
    If it does, you are still stuck with my question - when and how do you want to get the return value from the slot?
    ==========================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.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: invoke methods in another thread and get return values

    Have you seen QFuture and QtConcurrent ?
    This does exactly what you ask in the thread title.

  5. #5
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: invoke methods in another thread and get return values

    Quote Originally Posted by high_flyer View Post
    If it does, you are still stuck with my question - when and how do you want to get the return value from the slot?
    As I did say, I use QMetaObject::invokeMethod().
    It is possible to get return value (see Q_RETURN_ARG macro), but only in Qt:irectConnection, i.e. slot will be running in the same thread as a caller object. But I need it running in another thread. It can be achieved only in queued connection, in this case invokeMethod() with the Q_RETURN_ARG is useless. But I can write some data in memory passing its pointer as a slot argument (using Qt::BlockingQueuedConnection, of course). But I thought there is another way similar to invokeMethod() with Q_RETURN_ARG.

    int someValue() is more beautiful than void giveMeSomeValue(int *where).


    Added after 11 minutes:


    Quote Originally Posted by stampede View Post
    This does exactly what you ask in the thread title.
    Can I select a particular QThread from global pool?
    Last edited by elk; 9th February 2011 at 13:37.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: invoke methods in another thread and get return values

    Try defining a slot that returns a value and see if the connection works.
    Yes, connection to a slot returning value works ok.
    Can I select a particular QThread from global pool?
    I don't know, why do you need a particular thread to run your code ?

  7. #7
    Join Date
    Jun 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: invoke methods in another thread and get return values

    Quote Originally Posted by stampede View Post
    I don't know, why do you need a particular thread to run your code ?
    Because that code interacts with a network (QTcpSocket)

Similar Threads

  1. Replies: 2
    Last Post: 8th January 2011, 11:24
  2. Invoke UI functions from a different thread ?
    By umanga in forum Qt Programming
    Replies: 4
    Last Post: 27th May 2010, 08:44
  3. Thread mutex lock and return value
    By ^NyAw^ in forum Qt Programming
    Replies: 5
    Last Post: 13th February 2010, 08:32
  4. Stored procedure return values problem
    By jgreetham in forum Qt Programming
    Replies: 7
    Last Post: 10th September 2007, 18:38
  5. how to program custom Dialogs with return values
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2006, 01:37

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.