Results 1 to 3 of 3

Thread: Develop an Chat Application as use QT

  1. #1
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Develop an Chat Application as use QT

    Hello, every one. I am a new member of QT. I am writing a Chat Application( as Pigin). But I have a trouble, I have displayed list nick chat. In my Project, I use thread extend QThread and QTcpSocket to receive response from server for update other GUI form, ex: add a new Nick or message from other friend. But
    my socket read data very bad (data usually lose). Here is my MyThread.h code
    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QString>
    6. using namespace std;
    7. class MyThread : public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit MyThread(QObject *parent = 0);
    12. protected:
    13. virtual void run();
    14.  
    15. signals:
    16. void updateChat(QString);// Signal to update form chat!
    17. // and another signal....
    18.  
    19. public slots:
    20. void Process();// function to process response from server
    21. };
    22.  
    23. #endif // MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 
    and here is file MyThread.cpp
    Qt Code:
    1. #include "mythread.h"
    2. #include <QDebug>
    3. #include "login.h"
    4. #include <QObject>
    5. MyThread::MyThread(QObject *parent) :
    6. QThread(parent)
    7. {
    8.  
    9. }
    10. void MyThread::run()
    11. {
    12. Process();
    13.  
    14. }
    15. void MyThread::Process()
    16. {
    17. QString comnand;
    18.  
    19. qDebug()<<"Running....";
    20. int re;
    21. while(true) {
    22. newSocket->socket->waitForReadyRead(9000000);
    23. command = QString::fromUtf8( newSocket->socket->readAll());
    24.  
    25. if(!comand.isEmpty()) {
    26. qDebug()<<"Command :"<<command;
    27. QStringList list = comand.split("@|@", QString::SkipEmptyParts);
    28. if(list.at(0).compare("CHAT")==0) {
    29. emit updateChat(comand);
    30. }
    31. // ..... and many other commands
    32. else {
    33. }
    34.  
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 
    and here is file chat.cpp to update content for box chat
    Qt Code:
    1. #include "chat.h"
    2. #include "ui_chat.h"
    3. #include "login.h"
    4. #include <QDebug>
    5. #include "global2.h"
    6. #include "list.h"
    7. Chat::Chat(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::Chat)
    10. {
    11. ui->setupUi(this);
    12. connect(mThread,SIGNAL(updateChat(QString)),this,SLOT(updateBoxChat(QString)));// connect to thread to update box chat
    13. }
    14.  
    15. Chat::~Chat()
    16. {
    17. delete ui;
    18. }
    19. QString Chat::getID()
    20. {
    21. return id;
    22. }
    23. void Chat::setID(QString s)
    24. {
    25. this->id=s;
    26. }
    27. QString Chat::getName()
    28. {
    29. return this->name;
    30. }
    31.  
    32. void Chat::setName(QString s)
    33. {
    34. name = s;
    35. }
    36.  
    37. void Chat::updateBoxChat(QString s)
    38. {
    39. qDebug()<<"Content :"<<s;
    40. QStringList temp=s.split("@|@");
    41. QString nickname=temp.at(1);
    42. nickname=nickname.trimmed();
    43. QString key = List::getIntance()->persons.key(nickname);
    44. QString id = this->getID();
    45. qDebug()<<"Key:"<<key;
    46. QString content=temp.at(2);
    47. bool hidden = this->isHidden();
    48. if(key.compare(id)==0) {
    49. this->setWindowTitle(nickname);
    50. ui->noidung->append(nickname+" " + content);
    51. if(hidden==false)
    52. this->show();
    53. }
    54. }
    55. void Chat::closeEvent(QCloseEvent *event)
    56. {
    57. QString s = this->getID();
    58. qDebug()<<this->isHidden();
    59. this->setHidden(true);
    60. qDebug()<<this->isHidden();
    61. }
    62. void Chat::on_Send_clicked()
    63. {
    64.  
    65. int row = List::getIntance()->listWidget->currentRow();
    66. QString userName = List::getIntance()->t[row];
    67. //qDebug()<<"ID :"<<userName;
    68. QString text="CHAT@|@"+userName+"@|@"+this->ui->textEdit->toPlainText();
    69. // qDebug()<<text;
    70. QByteArray textTemp(text.toUtf8());
    71. //qDebug()<<"Send Message to server!";
    72. QString s = "Me: "+ ui->textEdit->toPlainText();
    73. newSocket->socket->write(textTemp);
    74. this->ui->textEdit->clear();
    75. this->ui->noidung->append(s);
    76. this->ui->textEdit->setFocus();
    77. }
    To copy to clipboard, switch view to plain text mode 
    Please help me solve this problem! Thank you!
    Last edited by phuong_90; 4th November 2011 at 17:14.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Develop an Chat Application as use QT

    Hi,
    remember that receiving from a TCP socket you must check how many bytes you received.

    Here
    Qt Code:
    1. command = QString::fromUtf8( newSocket->socket->readAll());
    To copy to clipboard, switch view to plain text mode 
    you think that all data are received.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Develop an Chat Application as use QT

    I don't understand your idea? I had this code:
    Qt Code:
    1. command = QString::fromUtf8( newSocket->socket->readAll());
    To copy to clipboard, switch view to plain text mode 
    in my program . In class MyThread, Socket receive data from server so bad. Data usually lose. Do you have idea to solution-problem?

Similar Threads

  1. Chat application using web services
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2011, 19:52
  2. How to build a Chat application in Qt??
    By Gokulnathvc in forum Newbie
    Replies: 6
    Last Post: 23rd March 2011, 08:48
  3. Specialised Network Chat Application
    By TropicalPenguin in forum Jobs
    Replies: 0
    Last Post: 28th September 2010, 23:40
  4. Qt4 Gui application cannot write to file
    By flair-kun in forum Qt Programming
    Replies: 4
    Last Post: 19th December 2009, 10:21
  5. which class i should used to make chat application ??
    By kunalnandi in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2008, 10:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.