Results 1 to 6 of 6

Thread: problem in writing text with QFile

  1. #1
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default problem in writing text with QFile

    Qt Code:
    1. QFile file(fileName);
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream out(&file);
    4. QApplication::setOverrideCursor(Qt::WaitCursor);
    5. out << "TESTING";
    6. QApplication::restoreOverrideCursor();
    7. file.close();
    To copy to clipboard, switch view to plain text mode 

    after i run above code the text file bcome
    untitled.JPG

    pls help me slove this problem...
    thx

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in writing text with QFile

    Use QTextStream instead of QDataStream. The latter is for serialization.

  3. #3
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: problem in writing text with QFile

    i m going to use QTcpSocket to send a QByteArray(which carry a file's content) to receiver.
    i use QDataStream here, bcoz the file can be any type(text file, picture file and etc).

    wat i did in my sender program is:
    quint64 maxSize;
    QByteArray Data;
    QFile file(fileName);
    Data = file.read(maxSize);

    QTcpSocket *tcpSocket = new QTcpSocket;
    tcpSocket->abort();

    tcpSocket->connectToHost(targetIP,targetPort);

    QByteArray block;
    QDataStream out_s(&block,QIODevice::WriteOnly);
    out_s.setVersion(QDataStream::Qt_4_0);

    out_s << (quint64)0;
    out_s << Data;
    out_s.device()->seek(0);
    out_s << (quint64)(block.size() - sizeof(quint64));
    tcpSocket->write(block);
    tcpSocket->disconnect();
    is it any solution available?

    i should use quint64 or quint16 here?
    Last edited by wei243; 5th March 2007 at 16:11.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in writing text with QFile

    1. Seeking in a socket device doesn't make sense as it is not a random access device.
    2. If you use QDataStream, you don't need to send the size of QByteArray as QDataStream will do that for you. Remember that this is a serialization mechanism, not a simple binary stream.
    3. connectToHost() is an asynchronous call - you have to wait for the connection to be established before sending/receiving any data. The same goes for disconnectFromHost (I think you meant that and not disconnect(), right?).

  5. The following user says thank you to wysota for this useful post:

    wei243 (6th March 2007)

  6. #5
    Join Date
    Feb 2007
    Posts
    16
    Thanks
    9

    Default Re: problem in writing text with QFile

    thx wysota.

    QTextStream slove my 1st problem in writing text to a file.
    QFile file(filename);
    file.open(QIODevice::WriteOnly);
    QTextStream textstream(&file);
    textstream << text;
    i apply it in writing a picture file with QDataStream.
    but nothing appear in the picture file.
    wat should i do?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem in writing text with QFile

    Quote Originally Posted by wei243 View Post
    i apply it in writing a picture file with QDataStream.
    but nothing appear in the picture file.
    wat should i do?
    You can't use a text stream to handle binary data... I suggest you don't use streams at all with binaries or use QDataStream::readRawData and QDataStream::writeRawData instead of << and >> operators.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 14:36
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  4. Writing Text on a Rectangle!!!
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2006, 10:23
  5. Problem with inserting text into QTextEdit
    By xorrr in forum Qt Programming
    Replies: 0
    Last Post: 6th February 2006, 11:45

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.