Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Spanish/french characters not displayed properly in QTableWidget



  • Hello
    I have an application in which i am required to read a CSV file (Comma Separated values) and display the values in a QTableWidget. This works perfect with all English characters. However, for internationalisation, I am required to also display characters in different European languages like Spanish and French. So if the CSV File contains a non english character, it is not displayed properly in the QTableWidget. The characters are just seen as some symbols.
    How can this be done?
    Regards

    KK



  • What is the encoding of your CSV file? Did you set the loading codec accordingly when getting the data from the CSV?



  • @JohanSolo
    Thanks for your time to answer my question.
    I am unable to control the encoding of the CSV File. The CSV file can be created from any of the spreadsheet applications.
    Regarding the codec of my application, I am setting the codec in the main.c as follows:

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    

    I have also tried setting the codec to the text stream. My source code for reading the csv file and listing in a QTableWidget is as follows:

    void csvfileDialog::on_btn_listfile_clicked()
    {
        QString data;
        QFile importedCSV(ui->txt_csvfilename->text());
        QStringList rowOfData;
        QStringList rowData;
        if(ui->txt_delimiter->text().trimmed().isEmpty())
            ui->txt_delimiter->setText(",");
    
        data.clear();
        rowOfData.clear();
        rowData.clear();
        if (importedCSV.open(QFile::ReadOnly))
        {
            QTextStream csvstream(&importedCSV);
            csvstream.setCodec("UTF-8");
            data = csvstream.readAll();
    //        data = importedCSV.readAll();
            rowOfData = data.split("\n");
            importedCSV.close();
        }
        else
            return;
        rowData = rowOfData.at(0).split(ui->txt_delimiter->text().trimmed());
        ui->tab_csvlist->setColumnCount(rowData.count());
        ui->tab_csvlist->setRowCount(rowOfData.count()-1);
        for (int x = 0; x < rowOfData.size(); x++)
        {
            rowData = rowOfData.at(x).split(ui->txt_delimiter->text().trimmed());
            for (int y = 0; y < rowData.size(); y++)
            {
                QString tempstr = rowData.at(y);
                qDebug()<<tempstr.toUtf8();
                ui->tab_csvlist->setItem(x,y,new QTableWidgetItem(tempstr));
            }
        }
    }
    

    Unable to understand where am I wrong.



  • @TheCrowKaka said:

    @JohanSolo
    Thanks for your time to answer my question.
    I am unable to control the encoding of the CSV File. The CSV file can be created from any of the spreadsheet applications.

    If you're on Windows it's most likely any other encoding but UTF-8 which will be used by default. Better check (and set if needed) manually the encoding before trying to import the data in your program.


Log in to reply