Results 1 to 12 of 12

Thread: How to integrate asio::io_service with Qt event loop?

  1. #1
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default How to integrate asio::io_service with Qt event loop?

    Hi,

    I need to call asio::io_service:: poll_one() in Qt event loop.
    I googled similar questions, but they do not have distinct answers.
    Links to examples are welcome.

    Thanks.

  2. #2
    Join Date
    Jun 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to integrate asio::io_service with Qt event loop?

    Is there anybody alive?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to integrate asio::io_service with Qt event loop?

    Of course we're alive. Probably no one here has done what you're trying to do. Neither have I, but you could try this:

    Write your own loop in whatever QObject-based class needs to poll the asio instance. In this loop, call QCoreApplication:: processEvents() and then call your asio:: poll_one() method. It would probably be smart to implement the handler in such a way that it does not do any processing itself, but simply emits a signal with the arguments (or the arguments packed into a struct). This way, the actual handling of the poll is done within the Qt even loop.
    Last edited by d_stranz; 10th February 2013 at 19:07.

  4. #4
    Join Date
    Dec 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to integrate asio::io_service with Qt event loop?

    You may be interested in my qtasio. It's an implementation of QAbstractEventDispatcher for boost::asio.

    All you need is to add the following line before creating QApplication (usually it's in main()).

    Qt Code:
    1. QApplication::setEventDispatcher(new QAsioEventDispatcher(my_io_service));
    To copy to clipboard, switch view to plain text mode 

    It makes io_service being run together with qt application in one thread without additional latency and performance drop (like in solutions with calling io_service:oll() "from time to time").

    Unfortunately, my solution is for posix systems only, since it use asio:osix::stream_descriptor . Windows support may need completely different approach or quite similar - I don't really know.

  5. #5
    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: How to integrate asio::io_service with Qt event loop?

    I am not so sure that replacing the main thread's event dispatcher is a good idea for any application that is QGuiApplication or QApplication based.

    In these cases the event dispatcher for the main thread is usually provided by the platform abstraction plugin and might to a lot more than the base dispatcher for the current operating system (e.g. window system event integration)

    Depending on platform it might not even be a good idea for a QCoreApplication based program, since the used dispatcher can depend on build time and runtime choices (e.g. GLib event loop integration on Unix systems).

    Cheers,
    _

  6. #6
    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: How to integrate asio::io_service with Qt event loop?

    Quote Originally Posted by anda_skoa View Post
    I am not so sure that replacing the main thread's event dispatcher is a good idea for any application that is QGuiApplication or QApplication based.

    In these cases the event dispatcher for the main thread is usually provided by the platform abstraction plugin and might to a lot more than the base dispatcher for the current operating system (e.g. window system event integration)

    Depending on platform it might not even be a good idea for a QCoreApplication based program, since the used dispatcher can depend on build time and runtime choices (e.g. GLib event loop integration on Unix systems).
    I had exactly the same impression after reading the post and looking at the code.
    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.


  7. #7
    Join Date
    Dec 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to integrate asio::io_service with Qt event loop?

    Ok. So why there is a Glib version of QAbstractEventDispatcher? Why integration with Glib is better than with boost::asio?

  8. #8
    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: How to integrate asio::io_service with Qt event loop?

    Quote Originally Posted by peper0 View Post
    Ok. So why there is a Glib version of QAbstractEventDispatcher?
    Not sure what you mean.

    Most likely there was a need for it and somebody implemented and upstreamed it.

    Presumably because there are or were GLIb event loop based libraries that somebody wanted to use in a singlethreaded Qt application.

    Quote Originally Posted by peper0 View Post
    Why integration with Glib is better than with boost::asio?
    Do you mean "why is it better integrated" or "why does the integration work better"?

    Cheers,
    _

  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: How to integrate asio::io_service with Qt event loop?

    Quote Originally Posted by peper0 View Post
    Ok. So why there is a Glib version of QAbstractEventDispatcher? Why integration with Glib is better than with boost::asio?
    One thing could be that it integrates well with GStreamer.
    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
    Dec 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to integrate asio::io_service with Qt event loop?

    Sorry for being not clear enough.

    I proposed to integrate boost::asio "mainloop" with qt by the means of reimplementing event dispatcher. This is exactly how qt is integrated with Glib. You said, that my proposition is wrong because of some reasons:

    Quote Originally Posted by anda_skoa
    I am not so sure that replacing the main thread's event dispatcher is a good idea for any application that is QGuiApplication or QApplication based.

    In these cases the event dispatcher for the main thread is usually provided by the platform abstraction plugin and might to a lot more than the base dispatcher for the current operating system (e.g. window system event integration)

    Depending on platform it might not even be a good idea for a QCoreApplication based program, since the used dispatcher can depend on build time and runtime choices (e.g. GLib event loop integration on Unix systems).
    I understand that my solution is not very flexible and it would not work out-of-box on other platforms. I understand also, that my boost::asio integration contradict other external event loop integration (like GLib event loop). However, in my opinion, QEventDispatcherGlib has the same drawbacks, but it's not considered to be a bad idea.

    Therefore, I'm wondering why reimplementing QAbstractEventDispatcher for glib is a good idea and reimplementing it for boost::asio is wrong?

    I'm not very deeply involved in QT, so maybe I'm missing something obvious.

  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: How to integrate asio::io_service with Qt event loop?

    Glib integration was implemented for Qt (and made the default event loop) as an improvement over a bare unix event dispatcher as it is the most commonly used event loop on unix systems. Usually you'd want to complement that lib instead of replacing it.
    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.


  12. #12
    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: How to integrate asio::io_service with Qt event loop?

    Quote Originally Posted by peper0 View Post
    I proposed to integrate boost::asio "mainloop" with qt by the means of reimplementing event dispatcher. This is exactly how qt is integrated with Glib. You said, that my proposition is wrong because of some reasons:
    No, the troublesome part is not the event dispatcher implementation, but the way it gets "installed".
    In QGuiApplication program the QPA will be asked to create the main loop's event dispatcher, i.e. so that it can integrate window event handling when this is different to how the base platform dispatcher works.

    E.g. integrate with the X11 event system on Unix, or the libscreen system on QNX.

    Quote Originally Posted by peper0 View Post
    However, in my opinion, QEventDispatcherGlib has the same drawbacks, but it's not considered to be a bad idea.
    It would be a bad idea as well if it were to be used by overwriting the event dispatcher needed by the application.

    But it is not.

    Quote Originally Posted by peper0 View Post
    Therefore, I'm wondering why reimplementing QAbstractEventDispatcher for glib is a good idea and reimplementing it for boost::asio is wrong?
    The difference is how the dispatcher is integrated, not what it does.
    The boost::asio could even be better implementation wise for all that I know.

    Quote Originally Posted by peper0 View Post
    I'm not very deeply involved in QT, so maybe I'm missing something obvious.
    The approach of providing an application specific event dispatcher is fine for any secondary thread, but potentially highly problematic on the main thread.
    Especially in a gui application.

    Cheers,
    _

Similar Threads

  1. Not able to receive the custom event from the event loop
    By Jagadeeshakr in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2012, 11:47
  2. Qt/MFC Event Loop - stop MFC receiving event?
    By Kaylx in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2012, 18:57
  3. Replies: 4
    Last Post: 6th August 2011, 01:40
  4. Replies: 10
    Last Post: 15th January 2010, 14:35
  5. Replies: 0
    Last Post: 23rd October 2008, 12:43

Tags for this Thread

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.