Results 1 to 3 of 3

Thread: [SOLVED]Qt 5.7 string encoding issues

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default [SOLVED]Qt 5.7 string encoding issues

    Hey mates

    I read a value from database which contains special national characters,say "zażółć gęślą jaźń",and try to display it as a QString.
    In old days Qt4 there were setCodecForCString() and setCodecForTr() whose worked well,sadly these are obsolete in Qt 5.7.

    I've googled for a solution,but either found advices to use the above,obsolete functions,or some like QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("utf-8")),or ensure Qt Creator uses UTF-8 encoding.Unfortunately,nothing worked for me :/

    Therefore I'm asking you for help,how nowadays in Qt 5.7 national characters should be handled properly in an application?
    Last edited by MasterBLB; 4th August 2016 at 12:57.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.7 string encoding issues

    1. Which database ? What code page is used to connect to the database ?
    2. Show some code.

    Codec for CString and codec fo tr have nothing to read data from the database.

  3. #3
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Qt 5.7 string encoding issues

    Qt Code:
    1. Generator::Generator(const QString &databasePath)
    2. {
    3. QString name = databasePath;
    4. name = name.replace("\\","/");
    5. database = QSqlDatabase::addDatabase("QSQLITE");
    6. database.setDatabaseName(name);
    7. if(database.open())
    8. {
    9. Logger::info("Database " + databasePath + " opened successfuly.");
    10. }
    11. else
    12. {
    13. Logger::error("Couldn't open database " + databasePath + "! Error: " + database.lastError().text() + ". Application aborted.");
    14. exit(-2);
    15. }
    16. }
    17.  
    18. Generator::~Generator()
    19. {
    20. if (database.isOpen())
    21. {
    22. database.close();
    23. }
    24. }
    25.  
    26. void Generator::generateDictionaryFile()
    27. {
    28. QSqlQuery query(database);
    29. query.exec("select distinct ulica from AdresyKody_mid_mif");
    30. Logger::info("Last error: " + query.lastError().text());
    31. while(query.next())
    32. {
    33. Logger::info("Got from database: " + query.value(0).toString());//national characters here
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    But the issue is solved already.In other place I had code in Logger
    Qt Code:
    1. logFile.write(logText.toLatin1());
    To copy to clipboard, switch view to plain text mode 
    and as it turned out that was the source of my issues.After change to:
    Qt Code:
    1. logFile.write(logText.toUtf8());
    To copy to clipboard, switch view to plain text mode 
    all works fine.

    Sorry mates for bothering you for nothing.

Similar Threads

  1. QtScript encoding
    By folibis in forum Qt Programming
    Replies: 10
    Last Post: 21st October 2013, 22:33
  2. Encoding/decoding a string to URL and HTML
    By ultr in forum Qt Programming
    Replies: 4
    Last Post: 25th September 2010, 22:40
  3. std:string how to change into system:string?
    By yunpeng880 in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 09:51
  4. Character encoding issues
    By yagabey in forum Qt Programming
    Replies: 11
    Last Post: 15th December 2008, 23:20
  5. Encoding a QString to No.?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 30th August 2007, 20:08

Tags for this Thread

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.