Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encoding problem with Arabic language and Qt5.8 on Windows
Qt 6.11 is out! See what's new in the release blog

Encoding problem with Arabic language and Qt5.8 on Windows

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 2.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Script22
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Script22
      wrote on last edited by
      #6

      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()
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Isn't this the same question as this thread ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Isn't this the same question as this thread ?

          S Offline
          S Offline
          Script22
          wrote on last edited by
          #3

          @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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • S Offline
              S Offline
              Script22
              wrote on last edited by
              #5

              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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Script22
                wrote on last edited by
                #6

                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()
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved