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.

57 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.

  6. John Says:

    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. John Says:

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

  8. Dave Says:

    John,

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

    –Dave

  9. sajida Says:

    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?

    thanks

  10. Dave Says:

    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. przemon Says:

    Big thanx, short and comprehensive, great help.

  12. mihai Says:

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

  13. amit ranjan Says:

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

  14. Dave Says:

    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. Denis Says:

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

  16. Dave Says:

    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. amit Says:

    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. Dave Says:

    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. amit Says:

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

  20. Dave Says:

    Did you try using a different user name?

  21. amit Says:

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

  22. amit Says:

    ya, also tried with different name

  23. caodung Says:

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

  24. Ice Says:

    Thx. Good work!

  25. amit Says:

    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. Jay Says:

    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

    Jay

  27. SIFE Says:

    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. Milos Says:

    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. Indrajeet Says:

    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.

    Regards
    Indrajeet

  30. Enton Says:

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

  31. eric Says:

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

  32. Dave Says:

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

    –Dave

  33. Zafiris Says:

    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. :)
    Regards,
    Zafiris

  34. Dave Says:

    Zafiris: Sure. Have fun!

  35. eduar Says:

    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. Dave Says:

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

  37. Anna Says:

    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. Dave Says:

    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:

    http://doc.qt.nokia.com/latest/deployment.html

    –Dave

  39. Amin Says:

    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. Dave Says:

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

  41. Amin Says:

    Thanks Dave

  42. kal Says:

    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. kal Says:

    I forget to ask, is this chat program threaded?

  44. Dave Says:

    kal:

    1. Yes
    2. Yes
    3. No

  45. James Says:

    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. Dave Says:

    James: Yup, fine with me.

  47. ppcrao Says:

    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. Dave Says:

    Do you have this in your .pro file:

    QT += network

  49. Ysabelle Says:

    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. Dave Says:

    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.

  51. Coder Says:

    Thank You Dave….

  52. miss.student Says:

    Hi.. tnx Dave!!!!! :)
    I read this code, but I’m trying to write it without designer!!!
    but I have a big problem
    how can I replace this in mainwindow.cxx
    void MainWindow::connected()
    {
    // Flip over to the chat page:
    stackedWidget->setCurrentWidget(chatPage);

    // And send our username to the chat server.
    socket->write(QString(“/me:” + userLineEdit->text() + “\n”).toUtf8());
    }
    in fact I dont know how open “chatpage” when I did’nt use designer??

  53. Dave Says:

    You would have to instantiate a QStackedWidget in your C++ code, and add different widgets as pages to it. Each widget would probably be a QFrame with the content you want inside it.

    This really is a lot easier to do with Designer if you can.

  54. miss.student Says:

    thanks very much,I try it…
    I want it for university project,it must be without designer!!

    maybe it is so easier for me to write it with UDP
    but it can’t has log in facility…

  55. Dave Says:

    It seems to me like you need to learn much more about UDP before you say something like UDP “can’t has log in facility”. That is simply not true.

  56. miss.student Says:

    Ok! so it maens it is possible whit UDP
    surely I need to learn more about it
    in fact I study math at university :)
    could I continue discussion with you by email??

  57. Dave Says:

    There is plenty of material on the internet on this topic. Please don’t correspond with me directly by email, as that would prevent others from benefiting from the conversation in the future.

Leave a Reply