PDA

View Full Version : Develop an Chat Application as use QT



phuong_90
4th November 2011, 15:47
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
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QString>
using namespace std;
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0);
protected:
virtual void run();

signals:
void updateChat(QString);// Signal to update form chat!
// and another signal....

public slots:
void Process();// function to process response from server
};

#endif // MYTHREAD_H

and here is file MyThread.cpp


#include "mythread.h"
#include <QDebug>
#include "login.h"
#include <QObject>
MyThread::MyThread(QObject *parent) :
QThread(parent)
{

}
void MyThread::run()
{
Process();

}
void MyThread::Process()
{
QString comnand;

qDebug()<<"Running....";
int re;
while(true) {
newSocket->socket->waitForReadyRead(9000000);
command = QString::fromUtf8( newSocket->socket->readAll());

if(!comand.isEmpty()) {
qDebug()<<"Command :"<<command;
QStringList list = comand.split("@|@", QString::SkipEmptyParts);
if(list.at(0).compare("CHAT")==0) {
emit updateChat(comand);
}
// ..... and many other commands
else {
}

}
}



and here is file chat.cpp to update content for box chat


#include "chat.h"
#include "ui_chat.h"
#include "login.h"
#include <QDebug>
#include "global2.h"
#include "list.h"
Chat::Chat(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Chat)
{
ui->setupUi(this);
connect(mThread,SIGNAL(updateChat(QString)),this,S LOT(updateBoxChat(QString)));// connect to thread to update box chat
}

Chat::~Chat()
{
delete ui;
}
QString Chat::getID()
{
return id;
}
void Chat::setID(QString s)
{
this->id=s;
}
QString Chat::getName()
{
return this->name;
}

void Chat::setName(QString s)
{
name = s;
}

void Chat::updateBoxChat(QString s)
{
qDebug()<<"Content :"<<s;
QStringList temp=s.split("@|@");
QString nickname=temp.at(1);
nickname=nickname.trimmed();
QString key = List::getIntance()->persons.key(nickname);
QString id = this->getID();
qDebug()<<"Key:"<<key;
QString content=temp.at(2);
bool hidden = this->isHidden();
if(key.compare(id)==0) {
this->setWindowTitle(nickname);
ui->noidung->append(nickname+" " + content);
if(hidden==false)
this->show();
}
}
void Chat::closeEvent(QCloseEvent *event)
{
QString s = this->getID();
qDebug()<<this->isHidden();
this->setHidden(true);
qDebug()<<this->isHidden();
}
void Chat::on_Send_clicked()
{

int row = List::getIntance()->listWidget->currentRow();
QString userName = List::getIntance()->t[row];
//qDebug()<<"ID :"<<userName;
QString text="CHAT@|@"+userName+"@|@"+this->ui->textEdit->toPlainText();
// qDebug()<<text;
QByteArray textTemp(text.toUtf8());
//qDebug()<<"Send Message to server!";
QString s = "Me: "+ ui->textEdit->toPlainText();
newSocket->socket->write(textTemp);
this->ui->textEdit->clear();
this->ui->noidung->append(s);
this->ui->textEdit->setFocus();
}

Please help me solve this problem! Thank you!

mcosta
4th November 2011, 22:37
Hi,
remember that receiving from a TCP socket you must check how many bytes you received.

Here

command = QString::fromUtf8( newSocket->socket->readAll());
you think that all data are received.

phuong_90
5th November 2011, 03:04
I don't understand your idea? I had this code:
command = QString::fromUtf8( newSocket->socket->readAll()); in my program . In class MyThread, Socket receive data from server so bad. Data usually lose. Do you have idea to solution-problem?