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
Code:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QString>
using namespace std;
{
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
Code:
#include "mythread.h"
#include <QDebug>
#include "login.h"
#include <QObject>
MyThread
::MyThread(QObject *parent
) :{
}
void MyThread::run()
{
Process();
}
void MyThread::Process()
{
qDebug()<<"Running....";
int re;
while(true) {
newSocket->socket->waitForReadyRead(9000000);
command
= QString::fromUtf8( newSocket
->socket
->readAll
());
if(!comand.isEmpty()) {
qDebug()<<"Command :"<<command;
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
Code:
#include "chat.h"
#include "ui_chat.h"
#include "login.h"
#include <QDebug>
#include "global2.h"
#include "list.h"
ui(new Ui::Chat)
{
ui->setupUi(this);
connect(mThread,
SIGNAL(updateChat
(QString)),
this,
SLOT(updateBoxChat
(QString)));
// connect to thread to update box chat }
Chat::~Chat()
{
delete ui;
}
{
return id;
}
{
this->id=s;
}
{
return this->name;
}
{
name = s;
}
void Chat
::updateBoxChat(QString s
) {
qDebug()<<"Content :"<<s;
nickname=nickname.trimmed();
QString key
= List
::getIntance()->persons.
key(nickname
);
qDebug()<<"Key:"<<key;
bool hidden = this->isHidden();
if(key.compare(id)==0) {
this->setWindowTitle(nickname);
ui->noidung->append(nickname+" " + content);
if(hidden==false)
this->show();
}
}
{
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;
//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!
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
Code:
command
= QString::fromUtf8( newSocket
->socket
->readAll
());
you think that all data are received.
Re: Develop an Chat Application as use QT
I don't understand your idea? I had this code:
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?