Qt query.value returns invalid characters (char)

I have a database field with a string like "PRODUCT N ° 1" if:

QString name = query.value(PRODUCT_INDEX_NAME).toString() 

I get: "PRODUCT N? 1." The degree sign has been lost.

I think this is a problem with char encoding. How to fix it?

Additional Information:

  • The database is Visual FoxPro (I don't know its char, maybe Windows-1252)
  • The request is made through QODBC
  • When an open database with dbfViewer appears in the dos view bar as “PRODUCT N | 1” and in the window is correctly visible as “PRODUCT N ° 1”.

Output: query.value(PRODUCT_INDEX_NAME).toByteArray() :

  "PRODUCT N?1 " QByteArray [0] 80 'P' char [1] 82 'R' char [2] 79 'O' char [3] 68 'D' char [4] 85 'U' char [5] 67 'C' char [6] 84 'T' char [7] 32 ' ' char [8] 78 'N' char [9] 63 '?' char [10]49 '1' char 

As you can see above, internally char '°' has already been converted to byte 63 ('?'), So the attempt suggested below "webclectic" does not work.

+4
source share
2 answers

You can take a value in a QByteArray and then use the correct QTextCodec to convert it to a QString . For example, if the codec was Windows-1250, you could do:

 QTextCodec* windows1250Codec = QTextCodec::codecForName("Windows-1250"); QString name = windows1250Codec->toUnicode(query.value(PRODUCT_INDEX_NAME).toByteArray()); 

See the QTextCodec documentation for more information and a list of available codecs.

+2
source

Support for QString Unicode and ° (0xB0) - This is an invalid UTF-8 encoded byte for running character code.

Try changing the encoding of the database to UTF-8 when creating the database:

 CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 

If it still does not work, try also with utf16.

But if you cannot change the database, you need to configure the connection parameters in the source code of the Qt application:

 // db is the instance of QSqlDatabase db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1"); 

But this option may not be supported by the Qt Visual Fox Pro driver, since ISC_DPB_LC_CTYPE for InterBase, as shown on the page, http://doc.qt.io/archives/qt-4.7/sql-driver.html (see § QIBASE Unicode support and text encoding). I don't have Visual Foxpro documentation to find the name of the connection parameter for character encoding.

+1
source

Source: https://habr.com/ru/post/1401557/


All Articles