Results 1 to 3 of 3

Thread: Problem with QTextStream

  1. #1
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Problem with QTextStream

    EDIT:

    My problem is that canReadLine() returns FALSE even if there is text, I tried to append /n to my string and still the incoming text is not percieved as a "complete" line. Any ideas?

    details below (sorry for the lenghty text)

    I'm designing a program to communicate with a remote system. Server side, it reads messages that contain a header (a capital letter) followed by a space and then the appropriate data (i.e. A 12 ... A tells me what the client is sending, 12 is the value).

    I use the << operator to read a word from the stream.

    Qt Code:
    1. void readClient(){
    2.  
    3. QTextStream ts( this );
    4.  
    5. while ( canReadLine() ) {
    6.  
    7. QString str = "";
    8.  
    9. ts >> str;
    10.  
    11. if(str == "A"){
    12. ts >> str;
    13. emit logText( tr("Client > Audio code '%1' has been detected." ).arg(str) );
    14. emit audio();
    15. }
    16.  
    17. (...)
    18. }
    To copy to clipboard, switch view to plain text mode 

    If I use a simple client application from the QT tutorials to send messages from the client to the server, this code works perfectly.

    However, since the robot the client application will be running on doesn't need any sort of GUI and from what I understand you can't use the QSocket class unless you create a QApplication, I decided to use a simple tcp class I found on the internet for the client side app, which in theory does everything I need it to do.

    Again, using this code for the client app, I know the client connects to the server correctly, and I know the message gets there because ts.read() returns the appropriate string, but the canReadLine() function still returns FALSE.

    Here is my usage of the class ClientSocket.

    Qt Code:
    1. void sendAudioCode( string inCode){
    2. try{
    3. ClientSocket s( "localhost", 4242 );
    4. string outCode = "A ";
    5. outCode.append(inCode);
    6.  
    7. s << outCode;
    8. }
    9. catch ( SocketException& ) {}
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    Finally, if anyone wants to consult this class, here is where I found it:
    http://linuxgazette.net/issue74/tougher.html
    Last edited by eter; 21st November 2006 at 03:53.

  2. #2
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QTextStream

    You can't reliably use QIODevice::canReadLine(), which is a QByteArray based function, with QTextStream's readLine(). In Qt 3 this works only sometimes for Latin-1 encoded text, because QIODevice::canReadLine() searches for the '\n' character in the raw data, and that happens to map to a '\n' unicode character using latin-1. For other character encodings, it breaks horribly. And in Qt 4, it doesn't work at all.

    If you know your data is latin-1 or octet-based (which is often the case for sockets), you should use QIODevice::readLine() instead.
    Last edited by Bitto; 21st November 2006 at 07:50.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  3. The following user says thank you to Bitto for this useful post:

    eter (21st November 2006)

  4. #3
    Join Date
    Oct 2006
    Posts
    3
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Problem with QTextStream

    Quote Originally Posted by Bitto View Post
    You can't reliably use QIODevice::canReadLine(), which is a QByteArray based function, with QTextStream's readLine(). In Qt 3 this works only sometimes for Latin-1 encoded text, because QIODevice::canReadLine() searches for the '\n' character in the raw data, and that happens to map to a '\n' unicode character using latin-1. For other character encodings, it breaks horribly. And in Qt 4, it doesn't work at all.

    If you know your data is latin-1 or octet-based (which is often the case for sockets), you should use QIODevice::readLine() instead.
    thanks for the clarification.

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.