Results 1 to 6 of 6

Thread: Waiting for signal and getting result

  1. #1
    Join Date
    Apr 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Waiting for signal and getting result

    Hello :-). I'm wondering if it's possible to get. result from signal lets say I've got onFinished(bool) signal and I'd like to make synchronous function that waits for this signal and returns bool from slot argument. It's possible to wait using QEventLoop but therr. is no way to return value. QFuture is what I need but it only works with QtConcurrent. I'd need something like QFuture but with possibility to manually set result.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for signal and getting result

    While it is usually not a good idea to simulate blocking with a nested event loop, I don't see why that would make it impossible to return anything.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    midas643 (8th April 2016)

  4. #3
    Join Date
    Apr 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Waiting for signal and getting result

    Is it possible? Could you propose some kind of solution? That would be good because I could do everything linear then. Like:

    Qt Code:
    1. if(asyncCallMadeSynchronous()) // returning result of slot or something like that
    2. {
    3. doSomethingElse();
    4. }
    To copy to clipboard, switch view to plain text mode 

    One solution is to store some kind of variable in my class like result, set it inside slot and return it after eventLoop.exec(); but I don't think it's pretty and would work as expected.

    with signals I have to wrap all this code inside slots and write specific ifs and this is usually more lines to write and I can't really find a good design for this. But that's fine if it's impossible. I will have to find some other way to write my code.

    In C# there's something like TaskCompletionSource to manually control task state(in Qt, task is similar to QFuture). And I'm looking for something similar to reproduce it.
    I was also thinking to extend QFuture somehow, to try re-implement TaskCompletionSource.

    Any ideas ?
    Last edited by midas643; 8th April 2016 at 12:03.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for signal and getting result

    Quote Originally Posted by midas643 View Post
    Qt Code:
    1. if(asyncCallMadeSynchronous()) // returning result of slot or something like that
    2. {
    3. doSomethingElse();
    4. }
    To copy to clipboard, switch view to plain text mode 
    As long as you are aware that this is not really blocking synchronous and you can deal with the potential reentrancy.
    Experience says that a lot of people who write such things are super surprised when functions like that are called twice without having finished yet, etc.

    Quote Originally Posted by midas643 View Post
    One solution is to store some kind of variable in my class like result, set it inside slot and return it after eventLoop.exec(); but I don't think it's pretty and would work as expected.
    Yes, that would definitely work.
    Or you connect to a lambda that captures a local variable by reference (and disconnect once you are done) or you use QSignalSpy, or a receiver object that combines your suggestion and the nested event loop.

    Quote Originally Posted by midas643 View Post
    I was also thinking to extend QFuture somehow, to try re-implement TaskCompletionSource.
    You can also return a "job" object that you can "exec" like a dialog.

    But again, in all these sases make sure you are aware of the consequences of nested event loops.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    midas643 (8th April 2016)

  7. #5
    Join Date
    Apr 2016
    Posts
    4
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Waiting for signal and getting result

    Quote Originally Posted by anda_skoa View Post
    Or you connect to a lambda that captures a local variable by reference (and disconnect once you are done) or you use QSignalSpy, or a receiver object that combines your suggestion and the nested event loop.
    _
    QSignalSpy seems nice. I don't really get capturing local variable by reference.

    Quote Originally Posted by anda_skoa View Post
    You can also return a "job" object that you can "exec" like a dialog.
    _
    What's the "job" object? Should I make my own class for that?
    I've noticed that:

    int QEventLoop::exec ( ProcessEventsFlags flags = AllEvents )
    Enters the main event loop and waits until exit() is called. Returns the value that was passed to exit().
    Can I pass the value to exit SLOT using some signal ?
    OK. I can't, it's possible with exit method though.

    Quote Originally Posted by anda_skoa View Post
    But again, in all these sases make sure you are aware of the consequences of nested event loops.
    _
    What are the consequences of nested event loops ?

    Thanks for responses very very helpful.
    Last edited by midas643; 8th April 2016 at 14:04.

  8. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Waiting for signal and getting result

    Quote Originally Posted by midas643 View Post
    QSignalSpy seems nice. I don't really get capturing local variable by reference.
    Something like this:
    Qt Code:
    1. bool result = false;
    2. auto connectionHandle = connect(sender, &Sender::onFinished, [&result, &l](bool b) { result = b; l.quit(); });
    3. l.exec();
    4. disconnect(connectionHandle);
    5. return result;
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by midas643 View Post
    What's the "job" object? Should I make my own class for that?
    Yes, a job would be an object of a class that encapsulates all data and state for an asynchronous operation.
    See for example QNetworkReply.

    Quote Originally Posted by midas643 View Post
    Can I pass the value to exit SLOT using some signal ?
    Not directly, QEventLoop::exit() is not a slot.
    Of course you can use a slot, function or lambda that gets connected to and which then calls exit.

    Quote Originally Posted by midas643 View Post
    What are the consequences of nested event loops ?
    As I said above: a nested event loop can lead to reentrancy-like situations.
    The call appears to be synchronous to a reader but of course it is not.
    Other events (user interaction, timers, sockets, etc) can lead to the method being called again, or the object that has this method being destroyed, etc.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    midas643 (8th April 2016)

Similar Threads

  1. waiting for gui to close
    By mustermann.klaus@gmx.de in forum Newbie
    Replies: 1
    Last Post: 23rd May 2015, 17:58
  2. Replies: 10
    Last Post: 9th July 2009, 12:05
  3. Replies: 1
    Last Post: 9th July 2009, 08:59
  4. Waiting on a thread?
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2007, 16:13
  5. Waiting for something
    By JonathanForQT4 in forum Newbie
    Replies: 20
    Last Post: 2nd May 2007, 17:35

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.