Results 1 to 11 of 11

Thread: invoke methods in another thread and get return values

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

    Default invoke methods in another thread and get return values

    Hello!

    Suppose a class (derived from QObject) with methods such as
    Qt Code:
    1. int receiveValue()
    To copy to clipboard, switch view to plain text mode 

    The class' events are processed in QThread. There are methods without returning value which may be called asynchronous. The values are needed to be given in similar way as using explicit method invocation but must not be running in caller thread.

    It is unable to QMetaObject::invokeMethod with return values in queued connections. The only solution I found is declare methods like
    Qt Code:
    1. void receiveValue(int *ret)
    To copy to clipboard, switch view to plain text mode 
    and invoke methods in Qt::BlockingQueuedConnection. But it is not so beautiful.

    Is there another solution alike QMetaObject::invokeMethod(), but with return values and queued connections?

    In addition to aesthetic, I have an academic interest in this problem.

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

    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.

  3. #3
    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.

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

    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.

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

    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.

  6. #6
    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.

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

    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 ?

  8. #8
    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)

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    You can send (not post!) an event to an object living in another thread. The event will be handled and if you provide a placeholder for the result in the event object you can then retrieve it in the code that sent the event.

    By the way, I don't see anything in Qt code that would technically prevent blocking-queued semantics from providing a return value so you might post a suggestion on Qt's bugtracker that such a situation should be allowed.
    Last edited by wysota; 9th February 2011 at 16:16.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

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

    Isn't easer (to code and to maintain) make slot to emit a signal with a result?
    Then receiving a result from another thread would be quite simple.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

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

    It is technically possible but it is against most object oriented programming rules.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.