Results 1 to 10 of 10

Thread: How to pass a QString to another class ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to pass a QString to another class ?

    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:
    Qt Code:
    1. messageIn->parseMessage(8);
    To copy to clipboard, switch view to plain text mode 


    The MessageIn class receives this call:
    Qt Code:
    1. void MessageIn::parseMessage(int dataIn)
    2. {
    3. //something here
    4. }
    To copy to clipboard, switch view to plain text mode 


    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 ?

  2. #2
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to pass a QString to another class ?

    This seems very simple; may be I'm not understanding you question


    If you want to change contents of string directly, then
    Qt Code:
    1. void MessageIn::parseMessage(QString &str){
    2. /////////
    3. }
    To copy to clipboard, switch view to plain text mode 

    If you want to return changed value, then
    Qt Code:
    1. QString MessageIn::parseMessage(const QString &str){
    2. /////////
    3. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QString &MessageIn::parseMessage(const QString &str){
    2. /////////
    3. }
    To copy to clipboard, switch view to plain text mode 

    If you just want to receive value, then
    Qt Code:
    1. void MessageIn::parseMessage(const QString &str){
    2. /////////
    3. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    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 ?

  4. #4
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    It should be very simple....

    in Class ClienThread I have this:
    Qt Code:
    1. void ClientThread::readData(){
    2. dataIn.clear();
    3. dataIn=(_tcpSocket->readAll());
    4. textBrowser->append(dataIn);
    5. [B]messageIn->parseMessage(dataIn);[/B]
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    In class MessageIn I have:
    Qt Code:
    1. void MessageIn::parseMessage(QString &dataIn){
    2. }
    To copy to clipboard, switch view to plain text mode 

    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 ?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    Quote Originally Posted by probine View Post
    I just want to work in the copy !!!
    Then use this:
    Qt Code:
    1. void MessageIn::parseMessage(QString dataIn){ ... }
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    ok, not working... here again in detail:

    Class ClientThread.h
    Qt Code:
    1. ..
    2. QString * dataIn;
    3. ..
    To copy to clipboard, switch view to plain text mode 

    Class ClientThread.cpp
    Qt Code:
    1. ..
    2. void ClientThread::readData(){
    3. dataIn->clear();
    4. dataIn->append(_tcpSocket->readAll());
    5. textBrowser->append(dataIn->toAscii());
    6. messageIn->parseMessage(*dataIn);
    7. }
    8. ..
    To copy to clipboard, switch view to plain text mode 

    Class MessageIn.h
    Qt Code:
    1. ..
    2. void parseMessage(Qstring dataIn);
    3. ..
    To copy to clipboard, switch view to plain text mode 

    Class MessageIn.cpp
    Qt Code:
    1. ..
    2. void MessageIn::parseMessage(Qstring dataIn){
    3. }
    4. ..
    To copy to clipboard, switch view to plain text mode 

    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 ?

  7. #7
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    According to me...
    Qt Code:
    1. messagein.h:10: error: variable or field `parseMessage' declared void
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. messagein.h:10: error: expected `;' before '(' token
    To copy to clipboard, switch view to plain text mode 
    This message probably points you that you didn't put a ";" at the end bracket of your class definition. E.G.:
    Qt Code:
    1. class A { ... }
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. class A { ... };
    To copy to clipboard, switch view to plain text mode 

    Hope it will help,
    Pierre.

  8. #8
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    Quote Originally Posted by probine View Post
    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?

  10. #10
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to pass a QString to another class ?

    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.

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  2. Replies: 2
    Last Post: 4th May 2006, 19:17
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 20:41
  5. how can I pass a QString to vsnprintf?
    By coralbird in forum Newbie
    Replies: 6
    Last Post: 18th January 2006, 08:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.