Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing a Chat Application in C++
Forum Updated to NodeBB v4.3 + New Features

Writing a Chat Application in C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pokemon123
    wrote on last edited by
    #1

    Hello,
    I am trying to make a chat application. I use Qt (for the GUI) and Boost.Asio (for socket programming function calls).
    I want to establish a connection between the client and server before I show the client's GUI. However, when I set up the connection, I use a function called io_service.run() which is a blocking call. Basically, it is an event loop queueing and executing read and write messages. Since this is a blocking call, it will not reach a.exec() which shows the GUI. So I end up with a black window, instead of the GUI.

    How do I fix this?
    Thank you. :)

    Pl45m4P 1 Reply Last reply
    0
    • P pokemon123

      Hello,
      I am trying to make a chat application. I use Qt (for the GUI) and Boost.Asio (for socket programming function calls).
      I want to establish a connection between the client and server before I show the client's GUI. However, when I set up the connection, I use a function called io_service.run() which is a blocking call. Basically, it is an event loop queueing and executing read and write messages. Since this is a blocking call, it will not reach a.exec() which shows the GUI. So I end up with a black window, instead of the GUI.

      How do I fix this?
      Thank you. :)

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @pokemon123

      Why you want to use Asio? You can do the whole Chat App (GUI + network connection) with Qt only.
      https://doc.qt.io/qt-5/qtnetwork-network-chat-example.html

      a.exec() will start its own event loop for GUI, so it's not that easy to make io_service.run() work with Qt.

      You could try to run your io_service in a different boost::thread, but I don't know, if everything will work as expected


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      3
      • KH-219DesignK Offline
        KH-219DesignK Offline
        KH-219Design
        wrote on last edited by
        #3

        Another option (if you are bound and determined to use as few threads as humanly possible, for whatever reason you may have):

        You can use a QTimer that "ticks" every N milliseconds (you choose N), and you can invoke other methods on your io_service instance instead of invoking run(). For example you could use io_service::run_for to constrain the length of "timeslice" you yield to the io_service.

        There are a number of similar methods, such as run_one, poll, and poll_one.

        In this way, you can achieve a sort of cooperative, voluntary yielding, time-slicing where the io_service uses the main/GUI thread, but it never blocks it for very long.

        www.219design.com
        Software | Electrical | Mechanical | Product Design

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved