I see what you are saying, and I have already subclassed tcpSocket, but I don't see an easy way to connect the signal to the appropriate slot.
The code is really simple, its just split into many files.
MSocket.cpp
#include "MSocket.h"
void MSocket::recv() {
inChat = in.readLine();
//chatOutput->append(inChat); <- this can't work.
}
#include "MSocket.h"
void MSocket::recv() {
QTextStream in(this);
QString inChat;
inChat = in.readLine();
//chatOutput->append(inChat); <- this can't work.
}
To copy to clipboard, switch view to plain text mode
MServer.cpp
#include "MServer.h"
}
void MServer::incomingConnection(int socketDescriptor) {
MSocket* socket = new MSocket(this);
socket->setSocketDescriptor(socketDescriptor);
}
#include "MServer.h"
MServer::MServer(QObject* parent) : QTcpServer(parent) {
}
void MServer::incomingConnection(int socketDescriptor) {
MSocket* socket = new MSocket(this);
socket->setSocketDescriptor(socketDescriptor);
}
To copy to clipboard, switch view to plain text mode
MChat.cpp
#include "MChat.h"
#include <QMessageBox>
setupUi(this);
//connections
connect(inputChat, SIGNAL(pressedReturn()),
sendButton, SLOT(animateClick()));
}
void MChat::on_actionConnect_activated() {
if (NULL == tcpServer) {
tcpServer = new MServer(this);
connect(tcpServer, SIGNAL(inText(QString&)), this,
SLOT(outputText(QString&)));
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}
}
}
}
#include "MChat.h"
#include <QMessageBox>
MChat::MChat(QMainWindow *parent) : QMainWindow(parent), tcpServer(NULL) {
setupUi(this);
//connections
connect(inputChat, SIGNAL(pressedReturn()),
sendButton, SLOT(animateClick()));
}
void MChat::on_actionConnect_activated() {
if (NULL == tcpServer) {
tcpServer = new MServer(this);
connect(tcpServer, SIGNAL(inText(QString&)), this,
SLOT(outputText(QString&)));
if (!tcpServer->listen(QHostAddress::Any, 43545)) {
QMessageBox::critical(this, tr("MChat"),
tr("Unable to start the server: %1.")
.arg(tcpServer->errorString()));
close();
return;
}
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks