Example Qt Chat Program

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.

88 comments to “Example Qt Chat Program”

You can leave a reply or Trackback this post.
  1. 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. I have the program up and running but I cant login=(

  3. 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. http://Metin says: -#1

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

  5. http://Metin says: -#1

    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.

  6. Hello, thanks for the Example Qt Chat Program.

    Is it possible to make it work over the internet?
    Lets say in my computer, I run the server and a friend of mine runs the client.

    The server runs in localhost, how to change this?

  7. Why are you claiming this source code as your own when it is part of the examples in the Qt library?

  8. John,

    Check your information. This code is *not* from the Qt examples. I wrote it.


  9. http://sajida says: -#1

    Hi….thank for your post….I learnt a lot from your article…I just want to know about the login screen qFrmae….how can you add light blue rounded frame? is it a image or you create it through other software?


  10. sajida,

    The login QFrame is stylized using Qt stylesheets in the .ui file. There are no images. To see the stylesheet, open the .ui file in Designer and right click on the top node of the object tree (located in the top left), and select “Change styleSheet”. There you will see code that looks like CSS, with things like “border-radius: 10px;” for example.

    Hope that helps.

  11. http://przemon says: -#1

    Big thanx, short and comprehensive, great help.

  12. http://mihai says: -#1

    I have a .ui file designed by me in qt creator,how i put it to your chat program?

  13. http://amit%20ranjan says: -#1

    it is loadedand run but i am not able login what is its servername and user name

  14. amit: Run the server and run the client. In the client, enter localhost for the server name, and you will be able to login.

    Remember, this is just an example, and not something useful for production.

  15. http://Denis says: -#1

    Hello! Thanks!
    Is it work only local?
    Can I run it over internet?

  16. Yes, it can run over the Internet. To run over the internet, run the server on one computer and run the client on a different computer. In the client, enter the hostname (or IP address) of the server computer. However, it will not work if you run the server behind a NAT router (like most DSL and cable modems).

  17. http://amit says: -#1

    this program is working only on single system, if i want to run it in two system for chatting, which is connected on network the what to do?

  18. amit: Run the client on one system and the server on another system. In the client, enter the IP address (or hostname) of the server. You can run as many clients as you want.

  19. http://amit says: -#1

    i tried but it is not working. error is displaying. error is already connection/connected

  20. Did you try using a different user name?

  21. http://amit says: -#1

    Dave: I tried but it is giving error ” QAbstractSocket:: connecttohost() already connecting/connected”.

  22. http://amit says: -#1

    ya, also tried with different name

  23. http://caodung says: -#1

    thank big, this example very good. it help me very much.i hope you will have many example as it.

  24. Thx. Good work!

  25. http://amit says: -#1

    Dave: thanx a lot, ur program help me a lot. in ur program there is one server and many client is communication. if u have any idea about server to server communication.

  26. http://Jay says: -#1

    Hi Dave,
    Thanks so much for this program. Really helped me get my mind wrapped around some stuff.

    I have a problem however, i have tried to make my own program using your code as a reference. The problem is I can’t get the incomingConnection function to run? I have tried connecting to the newConnection signal but to no avail. Pls help me.. Thanks again for the program; it was really helpful


  27. Useful example, thanks. I face a little problem about converting QString to quint16, how I can do that? I tried ToInt, ToSInt, ToShort, but none of theme work.

  28. http://Milos says: -#1

    very useful example, thanks, I have project at faculty and need chatbox in my application (not only chatbox, also i will need to exchange more infromation…) and found this example excellent to view “how it works”, but I have to ask you is it possible not to run server extern, to be integrated in main application?

    maybe to use tread, and one tread to create(run) server… I’ve never used treads, can you help me with idea how to do this?

  29. http://Indrajeet says: -#1

    Hi Dave

    I am new to QT.
    I am using Windows7 OS,Visualstudio2008,QT4.7.1.
    I am unable develop QTServer Library.
    Can u just tell me how to develop it.
    I want to add just 2 functions in that library FileRead & FileWrite.


  30. Plz teach me to create AI Chatbot on QT. plzzzzz need for my project……. GBU

  31. http://eric says: -#1

    why do you put some code here from qt library and say it’s yours? are you a thief?

  32. Eric, you are mistaken. This is my original work, and not from the Qt examples. Read it.


  33. http://Zafiris says: -#1

    Hi Dave,
    I am a Computer Science student and I was wondering if I could use a part of your code for my final project of my course.
    I know it’s a typical question but I feel that I have to ask. :)

  34. Zafiris: Sure. Have fun!

  35. http://eduar says: -#1

    Hi Smith, I would like to know how to view camera images online qt, like a video chat, is it possible?
    thank you very much

  36. eduar: Use QHttp to download online images, and use QImage to display them.

  37. http://Anna says: -#1

    How do you create a standalone executable? I’ve seen lots of sites saying to “statically link” by running various commands in a prompt, but I don’t know what that means or where to run the commands. I’m totally new and unclear on how to get a final executable or package that I can distribute to others.

  38. Anna:

    In order to build a standalone executable, on Linux, you’ll have to use a static version of Qt. This means your Qt libraries look like ‘libQtCore4.a” instead of “libQtCore4.so”.

    On Windows, the Qt libraries would be “libQtCore4.lib” instead of “libQtCore4.dll”.

    By default, Qt comes in non-static mode (.so and .dll), so you would have to re-compile Qt.

    To consider all your options for deploying Qt applications, start here:



  39. Hi , Dave

    Please give some useful tips :

    If we want convert this program to instant messaging and also the users at first , register in server at secure manner ( for example RSA)
    and then 2 client use a shared key for chat.

    Thanks in Advance

  40. Amin: This example is not suitable for a starting point for what you want to do.

  41. http://Amin says: -#1

    Thanks Dave

  42. can i add on to your program that is make it better and then merge it into my commercial program? if not then can i include it in to my freeware program?

  43. I forget to ask, is this chat program threaded?

  44. kal:

    1. Yes
    2. Yes
    3. No

  45. Hello

    I am learning Qt TCP and UDP. Would it be alright if I modify your example code for my own uses? I might leave the result at github, and would of course acknowledge the origin in a README file.

  46. James: Yup, fine with me.

  47. 1>ChatterBoxServer.cpp
    1>.\ChatterBoxServer.cpp(3) : fatal error C1083: Cannot open include file: ‘QTcpSocket’: No such file or directory
    1>Build log was saved at “file://c:\chatterboxd\chatterboxd\debug\BuildLog.htm”
    1>chatterboxd – 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

    above error is coming when i am trying to buid it using visula stuio 2008. how can i rectify it?

  48. Do you have this in your .pro file:

    QT += network

  49. http://Ysabelle says: -#1

    Hey Dave,
    thanks for this Program, it helps a lot.

    First sry for my bad english, i hope you understand me :)

    I have a Question, we have a project named online WhiteBoard.

    In it many Clients(User) can paint together on the same White-Board. For that we want to use a Client Server App.

    My Question, how can we relize it that the Server gets and send the information of painting, the problem is, in a chat Program you send Strings, but not for painting.

  50. Ysabelle,

    Your program is very similar to this chat program. You will have to record user input and convert that to text or binary form to be transmitted to the server.

Comment pages