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;
{
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
#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
To copy to clipboard, switch view to plain text mode
and here is file MyThread.cpp
#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 {
}
}
}
#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 {
}
}
}
To copy to clipboard, switch view to plain text mode
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"
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();
}
#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,SLOT(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();
}
To copy to clipboard, switch view to plain text mode
Please help me solve this problem! Thank you!
Bookmarks