Results 1 to 6 of 6

Thread: QIODevice and QTextCodec?

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QIODevice and QTextCodec?

    Hi,

    i read files like this,

    Qt Code:
    1. string entire;
    2. char buf[1024];
    3. while (device->readLine(buf, sizeof(buf)) && !device->atEnd()) { //QIODevice = device
    4. entire += buf;
    5. //...
    6. }
    7. size_t length = entire.length();
    8.  
    9. for (size_t i = 0; i < length; ++i) {
    10. char c = entire[i];
    11. //...
    12. s.append(c);
    13. }
    To copy to clipboard, switch view to plain text mode 

    but my QSting ("s") looks dirty. The problem is the file can be coded in utf8, iso 8859-15 and a lot more.

    How can i read the file with the right codec, if the user choose the codec before?

    I have try QTextCodec::setCodecForCStrings( QTextCodec::codecForName( codec ) ) but i hink thats not right. Can you help me pleas?
    Last edited by whitefurrows; 25th November 2010 at 20:36.

  2. #2
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QIODevice and QTextCodec?

    Hi,

    to describe the problem exact, with QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("UTF-8")) is the result:
    Special character: ??,??,??,??,??,??,??,???

    and without QTextCodec::setCodecForCStrings():
    Special character: �,ä,�,ö,�,ü,� ,�

    The origin text is:
    Special character: Ä,ä,Ö,ö,Ü,ü,ß,€

    Can you help me now please?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice and QTextCodec?

    You need to know from which encoding to which you need to convert, and it seems you know how to convert between encodings, so what is the problem you have?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QIODevice and QTextCodec?

    The encoding for the files is known, but i don't know how can i use the coding to read the file. I need to encode at this place char c = entire[i]; Ther is no problem to use QChar if that mouch more easy to handle! You understand my problem?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice and QTextCodec?

    You understand my problem?
    Not really.
    Why don't you just do exactly as shown in QTextCodec documentation?
    If you know the file encoding, then I still fail to understand your problem, unless your encoding is not supported by QTextCodec.
    From the docs:
    Qt Code:
    1. QByteArray encodedString = "..."; //from the file
    2. QTextCodec *codec = QTextCodec::codecForName("KOI8-R");
    3. QString string = codec->toUnicode(encodedString);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Apr 2009
    Location
    Italy
    Posts
    70
    Thanks
    23
    Thanked 15 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QIODevice and QTextCodec?

    You may want to replace the QIODevice with a QTextStream and then use QTextStream::setCodec

Similar Threads

  1. Replies: 0
    Last Post: 23rd December 2009, 13:53
  2. QTextCodec bad encoding?
    By ComaWhite in forum Newbie
    Replies: 0
    Last Post: 18th September 2009, 06:15
  3. Question in QTextCodec.
    By morgana in forum Newbie
    Replies: 1
    Last Post: 21st September 2008, 16:29
  4. QTextCodec: using KOI8-R, probe failed (e0 ce ru)
    By hvengel in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2007, 21:35
  5. QTextCodec::setCodecForCStrings() in libraries
    By conexion2000 in forum Qt Programming
    Replies: 5
    Last Post: 10th August 2007, 21:21

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.