How to integrate Asio network library with Qt library?

I want to use Asio and Qt together? each of them has two methods that should be called, and each of them blocks, for example: Asio has io_service.run (), and qt has QCoreApplication.exec (), if I run one of them, then the other cannot be launched.

I have two solutions:

  • create another thread and call io_service.run () from it.
  • create a timer with a cycle of 100 ms and call io_service.poll () from the same thread that calls QCoreApplication.exec ().

Are these decisions correct?

+4
source share
2 answers

This is a simple solution and works on every platform, but has some minor flaws. You enter a delay in the asio part of your application, which may or may not be acceptable - it depends on your case.

You may be interested in my solution , which makes the real integration of mainloops asio and QT.

See also the following question for more information on this topic: How to integrate the main Boost.Asio loop into a GUI infrastructure like Qt4 or GTK .

+2
source

You definitely want to peek into a multi-threaded application. Qt helps you with pretty good platform independent streaming classes . You also need to contact the message between objects belonging to different streams , and you also need to keep in mind that the picture is limited outside the flow of the graphical user interface .

0
source

Source: https://habr.com/ru/post/1379858/


All Articles