Example Qt Chat Program
July 9th, 2009A 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
- Open your favorite terminal (on Windows, use “Qt command prompt” in the start menu)
- cd into the chatterboxd directory
- Run qmake (you should see no output if it suceeds)
- Run make (or nmake on Windows — it will produce a lot of output and hopefully run error free)
- Run ./chatterboxd (or debug\chatterboxd.exe on Windows)
- 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.
September 10th, 2009 at 10:19 pm
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.
September 21st, 2009 at 9:06 pm
I have the program up and running but I cant login=(
September 21st, 2009 at 10:51 pm
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.
March 29th, 2010 at 3:02 am
Very good and informative example, I found it very usefull.
August 2nd, 2010 at 2:04 am
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.
September 29th, 2010 at 8:37 am
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?
December 1st, 2010 at 9:58 am
Why are you claiming this source code as your own when it is part of the examples in the Qt library?
December 1st, 2010 at 10:36 am
John,
Check your information. This code is *not* from the Qt examples. I wrote it.
–Dave
December 26th, 2010 at 10:47 pm
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
December 27th, 2010 at 3:03 am
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.
January 15th, 2011 at 3:20 pm
Big thanx, short and comprehensive, great help.
January 28th, 2011 at 5:06 am
I have a .ui file designed by me in qt creator,how i put it to your chat program?
thank’s
March 3rd, 2011 at 11:43 am
it is loadedand run but i am not able login what is its servername and user name
March 4th, 2011 at 6:33 am
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.
March 5th, 2011 at 3:34 pm
Hello! Thanks!
Is it work only local?
Can I run it over internet?
March 5th, 2011 at 3:46 pm
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).
April 5th, 2011 at 3:14 am
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?
April 5th, 2011 at 6:56 am
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.
April 5th, 2011 at 9:43 pm
i tried but it is not working. error is displaying. error is already connection/connected
April 5th, 2011 at 9:46 pm
Did you try using a different user name?
April 5th, 2011 at 11:31 pm
Dave: I tried but it is giving error ” QAbstractSocket:: connecttohost() already connecting/connected”.
April 5th, 2011 at 11:32 pm
ya, also tried with different name
April 11th, 2011 at 8:16 pm
thank big, this example very good. it help me very much.i hope you will have many example as it.
April 15th, 2011 at 4:11 am
Thx. Good work!
April 19th, 2011 at 11:22 am
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.
May 6th, 2011 at 5:28 pm
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
May 22nd, 2011 at 6:19 pm
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.
May 23rd, 2011 at 9:22 am
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?
June 8th, 2011 at 10:26 pm
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
July 22nd, 2011 at 7:04 am
Plz teach me to create AI Chatbot on QT. plzzzzz need for my project……. GBU
July 31st, 2011 at 5:19 pm
why do you put some code here from qt library and say it’s yours? are you a thief?
July 31st, 2011 at 6:41 pm
Eric, you are mistaken. This is my original work, and not from the Qt examples. Read it.
–Dave
August 1st, 2011 at 5:31 pm
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
August 1st, 2011 at 5:40 pm
Zafiris: Sure. Have fun!
August 8th, 2011 at 9:54 am
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
August 8th, 2011 at 9:58 am
eduar: Use QHttp to download online images, and use QImage to display them.
August 10th, 2011 at 2:05 am
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.
August 10th, 2011 at 6:22 am
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
August 10th, 2011 at 11:21 am
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
August 10th, 2011 at 11:23 am
Amin: This example is not suitable for a starting point for what you want to do.
August 10th, 2011 at 1:15 pm
Thanks Dave
August 11th, 2011 at 9:51 pm
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?
August 11th, 2011 at 9:53 pm
I forget to ask, is this chat program threaded?
August 11th, 2011 at 11:48 pm
kal:
1. Yes
2. Yes
3. No
September 8th, 2011 at 3:33 pm
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.
September 8th, 2011 at 3:37 pm
James: Yup, fine with me.
September 15th, 2011 at 11:38 pm
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?
September 15th, 2011 at 11:43 pm
Do you have this in your .pro file:
QT += network
December 12th, 2011 at 5:55 am
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.
December 12th, 2011 at 6:29 am
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.
December 14th, 2011 at 5:29 am
Thank You Dave….
January 19th, 2012 at 2:12 pm
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??
January 19th, 2012 at 2:15 pm
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.
January 19th, 2012 at 2:39 pm
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…
January 19th, 2012 at 2:41 pm
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.
January 19th, 2012 at 2:51 pm
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??
January 19th, 2012 at 2:52 pm
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.