Results 1 to 8 of 8

Thread: Read/Write from QTextStream

Hybrid View

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

    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.

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

    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.

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

    tonde (31st July 2008)

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

    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.

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

    Default Re: Read/Write from QTextStream

    can we use the seek function with a delimiter?

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

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

    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.

  7. 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
  •  
Qt is a trademark of The Qt Company.