PDA

View Full Version : readyRead slot - socket->readAll() data needed in separate class



ben1996123
9th December 2012, 01:45
I've made a client which can connect to a server, and it sends information to the server when a key is pressed (it's not a keylogger :o), and the data that gets sent back to the client with the readyRead slot is needed in a different class than the dialog class. Is there any simple way that I can move the data from one class to another without passing the ui as a parameter through lots of functions (and possibly causing lag)?

I have 2 classes: Dialog and clientSocket. The dialog class has a private pointer to a clientSocket object. In the dialog class, I have overridden the keyPressEvent function to send the key presses to the server, which sends back a number in the clientSocket class. This is what I need in the dialog class.

keyPressEvent function in dialog.cpp:


void Dialog::keyPressEvent(QKeyEvent *event){
if(socketTest->connectedToClient()){ //if connected, let the server handle input.
char a[1];
sprintf(a, "%c", event->key());
socketTest->socket->write(a); //socketTest is a pointer to a clientSocket object, socket is a QTcpSocket in the clientSocket class
}
//stuff

Server sends back "0", which is picked up in the readyRead function


void clientSocket::readyRead(){
QByteArray data = socket->readAll();
//what I need to do here is call a function from the dialog class
}

Thanks for any help.

amleto
9th December 2012, 02:00
Dialog::Dialog()
{
socketTest = ...;
connect(socketTest, signalX(QByteArray), this, slotABC(QByteArray));
}

void clientSocket::readyRead(){
QByteArray data = socket->readAll();
//what I need to do here is call a function from the dialog class

emit signalX(data);
}

ben1996123
9th December 2012, 02:33
code

Thanks, I completely forgot about signals and slots because I'm pretty new to Qt :)

anda_skoa
9th December 2012, 20:34
It might be an idea to do the socket stuff outside the dialog, maybe in the class that actually handles the response.

The dialog could just emit a signal with the relevant input and be totally independent of what your program does with it.

Makes it easier to test since you can use a different class for generating the input, e.g. something that reads input from a test file, etc.

Cheers,
_