Example Qt Chat Program

July 9th, 2009

A few months ago I gave a presentation to the BYU Unix Users Group on Qt GUI development. During the meeting, we created a simple chat room program called chatterbox. At the time I promised to post my source code with comments, a promise on which I am now making good.

Here is the code. Instructions for building and running it are below:

The client source code (chatterbox.zip)
The server source code (chatterboxd.zip)

Build Instructions

You’ll need to install Qt for your operating system, at least version 4.3 or newer. Make sure “qmake” is in your path. On Windows, you’ll also want Visual Studio 2008 Express. Then, download and unzip the two zip files above. I’ll wait here while you do that.

Building the Server

  1. Open your favorite terminal (on Windows, use “Qt command prompt” in the start menu)
  2. cd into the chatterboxd directory
  3. Run qmake (you should see no output if it suceeds)
  4. Run make (or nmake on Windows — it will produce a lot of output and hopefully run error free)
  5. Run ./chatterboxd (or debug\chatterboxd.exe on Windows)
  6. It should print Ready when it’s ready to go.

Building the Client

Build the client the same way. Note, it’s the chatterbox (no “d” on the end) directory.

What it Looks Like

Here’s what the client looks like when you run it:


Getting Help

If you can’t get this to build or are running into some other problem, just leave a comment below, and I’ll get to it as soon as possible.

5 Responses to “Example Qt Chat Program”

  1. awanish Says:

    Hi,
    the client chat what you have develop is multi casting and i want to change it to uni casting.
    please help me out.
    Thanks a lot.

  2. joan Says:

    I have the program up and running but I cant login=(

  3. Dave Says:

    Joan, did you remember to start the server, and did you type “localhost” into the client login form?

    awanish: This is a chat room application, not an instant messenger. It could be modified to be an instant messenger. You would have to modify the server to keep a map of QTcpSockets to usernames (QStrings), and then each client would have to send the username along with each message they want to send. The server could then route the messages appropriately. But seriously, this is just an example program, and if you want instant messaging, I strongly recommend something like Jabber.

  4. Metin Says:

    Very good and informative example, I found it very usefull.

  5. Metin Says:

    Dave, thanks for answering my e-mail, as you said by adding

    connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));

    to ChatterBoxServer::incomingConnection(int socketfd) function we can fix a memory leak.

Leave a Reply