Results 1 to 8 of 8

Thread: Read/Write from QTextStream

  1. #1
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Default Read/Write from QTextStream

    I've just started with QT and I found something I can't understand.

    Why won't this work?

    Qt Code:
    1. QTextStream out("test",QIODevice::ReadWrite);
    2. out<<"hello";
    3. QString string;
    4. out >> string;
    To copy to clipboard, switch view to plain text mode 

    Only "test" is added to the string. Couldn't find a way to append "hello" to the string.

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

    Default Re: Read/Write from QTextStream

    The docs say:
    QTextStream::QTextStream ( const QByteArray & array, QIODevice::OpenMode openMode = QIODevice::ReadOnly )
    Constructs a QTextStream that operates on array, using openMode to define the open mode. The array is accessed as read-only, regardless of the values in openMode.
    You have to use a different constructor to create an RW stream:
    Qt Code:
    1. QString buffer( "test" );
    2. QTextStream out( &buffer, QIODevice::ReadWrite );
    3. out << "hello";
    4. QString string;
    5. out >> string; // == "testhello"
    To copy to clipboard, switch view to plain text mode 

    If you just want to append a string use QString::operator+() or QString::append().

  3. #3
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Default Re: Read/Write from QTextStream

    Quote Originally Posted by jacek View Post
    The docs say:

    You have to use a different constructor to create an RW stream:
    Qt Code:
    1. QString buffer( "test" );
    2. QTextStream out( &buffer, QIODevice::ReadWrite );
    3. out << "hello";
    4. QString string;
    5. out >> string; // == "testhello"
    To copy to clipboard, switch view to plain text mode 
    OK, thanks! I tought that const string would use the "QString" constructor. Apparently it uses the "const ByteArray" constructor.

    If you just want to append a string use QString:perator+() or QString::append().
    My intention was to learn QTextStream...

  4. #4
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Default Re: Read/Write from QTextStream

    There is more to come
    It seems that if I try to assign QByteArray to the QTextStream QT is once again using the IODevice::ReadOnly constructor. How can I assign QByteArray to the QTextStream in ReadWrite mode? Threre should be one according to the documentation

    Qt Code:
    1. QString buffer = "test";
    2. QByteArray array;
    3. array.append(buffer);
    4. QTextStream out(&array,QIODevice::ReadWrite);
    5. out << "hello";
    6. QString string;
    7. out >> string; // == "test"
    To copy to clipboard, switch view to plain text mode 

    I even get similar problems when using IODevices:
    Qt Code:
    1. QTemporaryFile file("test");
    2. if (!file.open()) //will always be opened in QIODevice::ReadWrite mode
    3. return;
    4. QTextStream inOut(&file);
    5. inOut << "hello";
    6. inOut >> s; // == ""
    To copy to clipboard, switch view to plain text mode 

    What am I missing here?
    Last edited by tonde; 29th July 2008 at 09:08.

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

    Default Re: Read/Write from QTextStream

    For the file you need:
    Qt Code:
    1. QTextStream inOut(&file);
    2. inOut << "hello";
    3. inOut.flush(); // write to file
    4. inOut.device()->seek( 0 ); // rewind the file
    5. inOut >> s; // == "hello"
    To copy to clipboard, switch view to plain text mode 
    Unfortunately I don't know what's wrong with QByteArray case.

  6. The following user says thank you to jacek for this useful post:

    tonde (31st July 2008)

  7. #6
    Join Date
    Jul 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4

    Default Re: Read/Write from QTextStream

    Thanks. I missed that seek() function completely. Implementing seek() to the QByteArray case seems to be working.

    example:

    Qt Code:
    1. QString buffer = "funny world";
    2.  
    3. QByteArray array;
    4. array.append(buffer);
    5. QTextStream out(&array,QIODevice::ReadWrite);
    6.  
    7. //out.pos() == 0
    8. out << "hello"; //"hello" overwrites "funny"
    9.  
    10. out.seek(0); // set position to 0
    11.  
    12. QString string;
    13. QString string2;
    14.  
    15. // stream == "hello world"
    16. // QTextStream reads into strings word by word delimited by space
    17. out >> string; // == "hello"
    18. out >> string2; // == "world"
    To copy to clipboard, switch view to plain text mode 
    Last edited by tonde; 31st July 2008 at 11:21.

  8. #7
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read/Write from QTextStream

    can we use the seek function with a delimiter?

    e.g. |word1|word2|....
    with delimiter "|"

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

    Default Re: Read/Write from QTextStream

    Quote Originally Posted by SunnySan View Post
    can we use the seek function with a delimiter?
    No, you have to use QString::indexOf() or similar method.

  10. The following user says thank you to jacek for this useful post:

    SunnySan (31st July 2008)

Similar Threads

  1. QTextStream : Remove a Line?
    By kaydknight in forum Qt Programming
    Replies: 7
    Last Post: 31st January 2011, 19:15
  2. when close QTextStream
    By mattia in forum Newbie
    Replies: 1
    Last Post: 24th November 2007, 14:17
  3. Create QTextStream
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 21:25
  4. reading from QTextStream
    By matyi52 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 14th December 2006, 08:26
  5. QTextStream capture stdout from xsltParseStylesheetFile
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 25th June 2006, 12:24

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.