PDA

View Full Version : How to pass a QString to another class ?



probine
9th December 2006, 15:01
I must be doing something very wrong !!!

I am able to pass an integer from one class to another.

From class Client, I am passing this integer to class MessageIn:


messageIn->parseMessage(8);



The MessageIn class receives this call:


void MessageIn::parseMessage(int dataIn)
{
//something here
}



I want to do the exact same thing, but I want to pass a QString from Client to MessageIn.

How does the signature and expected variables for the functions should look like ?

yogeshm02
9th December 2006, 15:19
This seems very simple; may be I'm not understanding you question


If you want to change contents of string directly, then


void MessageIn::parseMessage(QString &str){
/////////
}


If you want to return changed value, then


QString MessageIn::parseMessage(const QString &str){
/////////
}

or


QString &MessageIn::parseMessage(const QString &str){
/////////
}


If you just want to receive value, then


void MessageIn::parseMessage(const QString &str){
/////////
}

probine
9th December 2006, 16:04
I guess the code you posted is passing by reference, so changes to the string in one class will affect the whole string.

How about passing a copy of the string, so I work in the copy ?

probine
9th December 2006, 16:16
It should be very simple....

in Class ClienThread I have this:


void ClientThread::readData(){
dataIn.clear();
dataIn=(_tcpSocket->readAll());
textBrowser->append(dataIn);
messageIn->parseMessage(dataIn);
}


The "dataIn" is a QString that has been declared earlier in the class.

In class MessageIn I have:


void MessageIn::parseMessage(QString &dataIn){
}


Class "MessageIn" has to receive the string from class "ClientThread". The string should be a copy of the original string, because the original will be cleared and I just want to work in the copy !!!

How ?

jacek
9th December 2006, 16:44
I just want to work in the copy !!!
Then use this:
void MessageIn::parseMessage(QString dataIn){ ... }
But beware that if you just invoke that method from client thread, it will be executed within that thread. If you want to pass data between threads better use signals and queued connections.

probine
9th December 2006, 16:54
ok, not working... here again in detail:

Class ClientThread.h


..
QString * dataIn;
..


Class ClientThread.cpp


..
void ClientThread::readData(){
dataIn->clear();
dataIn->append(_tcpSocket->readAll());
textBrowser->append(dataIn->toAscii());
messageIn->parseMessage(*dataIn);
}
..


Class MessageIn.h


..
void parseMessage(Qstring dataIn);
..


Class MessageIn.cpp


..
void MessageIn::parseMessage(Qstring dataIn){
}
..


This gives me the following error:

messagein.h:10: error: variable or field `parseMessage' declared void
messagein.h:10: error: expected `;' before '(' token
clientthread.cpp: In member function `void ClientThread::readData()':
clientthread.cpp:34: error: 'class MessageIn' has no member named 'parseMessage'
mingw32-make[1]: *** [release\clientthread.o] Error 1
mingw32-make[1]: Leaving directory `E:/infospeed/program/server'
mingw32-make: *** [release] Error 2

Any idea ?

hickscorp
9th December 2006, 17:36
According to me...

messagein.h:10: error: variable or field `parseMessage' declared void
This message most likely tells you that in your .h you declared the method "parseMessage" as void, and in your implementation you tried to return something or declared it with any other type.

Also:

messagein.h:10: error: expected `;' before '(' token
This message probably points you that you didn't put a ";" at the end bracket of your class definition. E.G.:

class A { ... }
instead of

class A { ... };

Hope it will help,
Pierre.

probine
9th December 2006, 17:40
That is not the problem... I can compile and execute the program without any problem if I just pass an integer from one class to the other, but as soon as I try to pass a QString or a string, then I get estrange error messages.

jacek
9th December 2006, 19:19
messagein.h:10: error: variable or field `parseMessage' declared void
messagein.h:10: error: expected `;' before '(' token
Are you sure that these are the first errors you get? Maybe you are missing #include directive somewhere?

probine
9th December 2006, 20:16
Thanks JACEK,

I included <QString> in all the classes.

I don't know why I have to included it through !!! I thought QString was a part of the core of Qt.