Encoding problem with Arabic language and Qt5.8 on Windows



  • I'm working on an app where it should view data from mysql database in a QTableView using QSqlQueryModel.

    I'm having hard time viewing the data properly on windows, however it works well under linux.

    I'm providing a compilable example and a sqldump of my example table

    This is the example pro file

    QT       += core gui sql
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = minimal
    TEMPLATE = app
    DEFINES += QT_DEPRECATED_WARNINGS
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    

    mainWindow header file

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainWindow CPP file

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlQuery>
    #include <QtSql/QSqlQueryModel>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost");
        db.setDatabaseName("test");
        db.setUserName("root");
        db.setPassword("123456");
        db.open();
        QSqlQueryModel *model;
        model = new QSqlQueryModel;
        model->setQuery("SELECT * FROM example");
        ui->tableView->setModel(model);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    

    and here's mysqldump output

    -- MySQL dump 10.13  Distrib 5.6.21, for Win32 (x86)
    --
    -- Host: localhost    Database: test
    -- ------------------------------------------------------
    -- Server version	5.6.21
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE='+00:00' */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
    --
    -- Table structure for table `example`
    --
    
    DROP TABLE IF EXISTS `example`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `example` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `firstName` text NOT NULL,
      `lastName` text NOT NULL,
      `job` text NOT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `example`
    --
    
    LOCK TABLES `example` WRITE;
    /*!40000 ALTER TABLE `example` DISABLE KEYS */;
    INSERT INTO `example` VALUES (1,'زيد نيس اشسبي','زيد نيس اشسبي','زيد نيس اشسبي'),(2,'زيد نيس اشسبي','زيد نيس اشسبي','زيد نيس اشسبي');
    /*!40000 ALTER TABLE `example` ENABLE KEYS */;
    UNLOCK TABLES;
    /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
    
    /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    
    -- Dump completed on 2017-09-22 13:49:06
    
    

    I am using UTF-8 for encoding the table and the fields.
    but the data shows up in weird symbols like this
    زيد نيس اشسبي

    any idea about how to get this working ?


  • Lifetime Qt Champion

    Hi,

    Isn't this the same question as this thread ?



  • @SGaist It's the same issue, but i added a compilable example for clarification and after I posted this I found out that I don't have enough privileges to delete the other thread


  • Lifetime Qt Champion

    Then just edit your original post and add a note to it that explains what you added.



  • I figured out how to view the data in the table view properly.

    But I still have an issue with the encoding

    for a simple example
    the output of these lines

        qDebug() << QString("م");
        qDebug() << QString::fromUtf8(QString("م").toLocal8Bit());
        qDebug() << QString("م").toLocal8Bit().toHex();
    
        qDebug() << QString("م").toUtf8().toHex();
        qDebug() << QString("م").toUtf8();
    
        qDebug() << QString::fromUtf8(QString("م").toLocal8Bit());
    

    is

    "?"
    "?"
    "3f"
    "d985"
    "\xD9\x85"
    "?"
    

    the encoding of the editor is UTF-8



  • I figured it out, in the Qt creator editor setting, i chose encoding to be system instead of UTF-8 and now everything works
    so the setup is as follows:

    MySql collation : UTF8_bin
    Editor encoding : System
    QStrings to be inserted to database : UTF-8 using

    QString::toUtf8()
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.