Results 1 to 12 of 12

Thread: Character encoding issues

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Character encoding issues

    Quote Originally Posted by yagabey View Post

    Qt Code:
    1. QTextCodec *codec=QTextCodec::codecForName("ISO 8859-9");
    To copy to clipboard, switch view to plain text mode 

    in the message box, characters are not correct again..(it shows "İ" instead of "İ" )
    Qt Code:
    1. QMessageBox::information(NULL, "Ausgabe", searchedAuthor);
    To copy to clipboard, switch view to plain text mode 

    what else should i do?
    So, the right codec is definetyly Codepage 857, which is not supported by Qt :-(

    You have to implement the conversion from CP 857 to ISO-8859-9 yourself.

    Just walk throught the byte array and convert chars>127:

    Qt Code:
    1. for (int i=0; i<auth_len; i++) {
    2. int c=author[i];
    3.  
    4. if (c<0) {
    5. c+=256;
    6.  
    7. switch(c) {
    8. // f.e. the g with a "bow" on it
    9. case 167: author[i]=240; break; // or maybe 240-256=-16, try it, I've got no turkish windows to test it!
    10. // I think you don't have to do it for all 128 chars, only for the few
    11. // turkish special chars you need
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    G.

  2. #2
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Character encoding issues

    Here is a summary of the code:


    Qt Code:
    1. #include <QHttp>
    2. #include <QUrl>
    3. #include <QBuffer>
    4. #include <QFile>
    5. #include <QTextStream>
    6. #include <QXmlStreamReader>
    7. #include <QHttp>
    8. #include <QByteArray>
    9.  
    10.  
    11. class ColumnListing
    12. {
    13. Q_OBJECT
    14. public:
    15. ColumnListing();
    16.  
    17. public slots:
    18. void fetch();
    19. void readData(const QHttpResponseHeader &);
    20.  
    21. private:
    22. void parseXml();
    23.  
    24. QXmlStreamReader xml;
    25. QString currentTag;
    26. QString linkString;
    27. QString titleString;
    28. QString descriptionString;
    29. QString authorString;
    30. QString urltext;
    31. QString inputNews;
    32. QString searchedAuthor;
    33. QFile file;
    34. QFile *htmlFile;
    35. QHttp httpInstance;
    36. int subconnectionId;
    37. QUrl urlColumnToGo;
    38.  
    39. QHttp http;
    40. int connectionId;
    41.  
    42. };
    To copy to clipboard, switch view to plain text mode 

    constructor part:
    Qt Code:
    1. ColumnListing::ColumnListing(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4.  
    5. connect(&http, SIGNAL(readyRead(const QHttpResponseHeader &)),
    6. this, SLOT(readData(const QHttpResponseHeader &)));
    7.  
    8. char *input;
    9. char *author;
    10. input = new char[100];
    11. author = new char[100];
    12.  
    13. cout<<"Newspaper ?\n";
    14. cin>>input;
    15. inputNews=input;//inputNews is global QString
    16.  
    17. cout<<"Author ?\n";
    18. cin>>author;
    19. searchedAuthor=author;//searchedAuthor is global QString
    20.  
    21. if (inputNews=="milliyet"){
    22. urltext.append("http://www.milliyet.com.tr/D/rss/rss/RssY.xml?ver=51");
    23. }
    24. else if (inputNews=="sabah"){
    25. urltext.append("http://www.sabah.com.tr/rss/yazarlar.xml");
    26. }
    27. else if (inputNews=="radikal"){
    28. urltext.append("http://www.radikal.com.tr/radikal_yazar.xml");
    29. }
    30. /*** More News sites here....***/
    31.  
    32. file.setFileName("output.txt");//file is global QFile
    33. if (!file.open(QFile::ReadWrite | QFile::Truncate))
    34. return;
    35.  
    36. htmlFile= new QFile("htmloutput.html");//htmlFile is global QFile
    37. if (!htmlFile->open(QFile::ReadWrite | QFile::Truncate))
    38. return;
    39. }
    To copy to clipboard, switch view to plain text mode 

    fetching:
    Qt Code:
    1. void ColumnListing::fetch()
    2. {
    3. xml.clear();
    4. QUrl url(urltext);
    5. http.setHost(url.host());
    6. connectionId = http.get(url.path());
    7. }
    To copy to clipboard, switch view to plain text mode 

    parsing rs doc:
    Qt Code:
    1. void ColumnListing::parseXml()
    2. {
    3. QTextStream inputText(&file);
    4.  
    5. while (!xml.atEnd()) {
    6. xml.readNext();
    7. if (xml.isStartElement()) {
    8. if (xml.name() == "item")
    9. linkString = xml.attributes().value("rss:about").toString();
    10. currentTag = xml.name().toString();
    11. } else if (xml.isEndElement()) {
    12. if (xml.name() == "item") {
    13.  
    14. if(authorString.contains(searchedAuthor,Qt::CaseInsensitive) ){
    15. inputText << titleString<< " "<<linkString <<" "<< descriptionString << authorString <<"\n";
    16.  
    17. QUrl url(linkString);
    18.  
    19. httpInstance.setHost(url.host());
    20. subconnectionId = http.get(url.path(),htmlFile);//write the author page into html file
    21. }
    22.  
    23. titleString.clear();
    24. linkString.clear();
    25. descriptionString.clear();
    26. authorString.clear();
    27.  
    28. }
    29.  
    30. } else if (xml.isCharacters() && !xml.isWhitespace()) {
    31. if (currentTag == "title"){
    32. titleString += xml.text().toString();
    33. }
    34. else if (currentTag == "link"){
    35. linkString += xml.text().toString();
    36. }
    37. else if (currentTag == "description"){
    38. descriptionString += xml.text().toString();
    39. }
    40. else if (currentTag == "dc:creator"){
    41. authorString += xml.text().toString();
    42. }
    43. }
    44. }
    45. if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
    46. qWarning() << "XML ERROR:" << xml.lineNumber() << ": " << xml.errorString();
    47. http.abort();
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. q = q->codecForName("ISO-8859-9");
    6. QTextCodec::setCodecForCStrings(q);
    7.  
    8. ColumnListing *columnlisting = new ColumnListing;
    9. columnlisting->fetch();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Character encoding issues

    Ok at last I made it work:

    instead of:
    Qt Code:
    1. searchedAuthor = QString::fromAscii(author);
    To copy to clipboard, switch view to plain text mode 

    I used :
    Qt Code:
    1. searchedAuthor = QString::fromUtf8(author);
    To copy to clipboard, switch view to plain text mode 

    and now everything works perfectly, thank you all...

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.