Results 1 to 7 of 7

Thread: Chowing Portuguese characters in a QTableView

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Chowing Portuguese characters in a QTableView

    Hi,
    I want to display characters like { 'ç', 'á', 'ã', ...} in a column of a QTableView, used with a QSqlTableModel to access data from the database.
    The original data was imported from a very large database and suffered some manipulation over a spreadsheet.
    Any ideas?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Chowing Portuguese characters in a QTableView

    Sorry, but what is you problem. I don't get your point. Since Qt uses UTF-8 by default there is no problem if you set the right character set on your database.
    So what have you tried or what is not exactly working?

  3. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Chowing Portuguese characters in a QTableView

    This is the script used to create the database:
    Qt Code:
    1. SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
    2. SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
    3. SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
    4.  
    5. CREATE SCHEMA IF NOT EXISTS `localidade` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin ;
    6. USE `localidade`;
    7.  
    8. -- -----------------------------------------------------
    9. -- Table `localidade`.`Localidade`
    10. -- -----------------------------------------------------
    11. DROP TABLE IF EXISTS `localidade`.`Localidade` ;
    12.  
    13. CREATE TABLE IF NOT EXISTS `localidade`.`Localidade` (
    14. `idLocalidade` INT NOT NULL ,
    15. `nomeLocalidade` VARCHAR(45) CHARACTER SET 'latin1' NOT NULL ,
    16. PRIMARY KEY (`idLocalidade`) )
    17. ENGINE = InnoDB
    18. DEFAULT CHARACTER SET = latin1
    19. ROW_FORMAT = DEFAULT;
    20.  
    21.  
    22.  
    23. SET SQL_MODE=@OLD_SQL_MODE;
    24. SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
    25. SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
    To copy to clipboard, switch view to plain text mode 

    If i insert data using the code i wrote and the Ui associated ... everything works fine.
    The data inserted with lines like this ones:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO Localidade VALUES (3750, 'ÁGUEDA')"); query.exec();
    3. query.prepare("INSERT INTO Localidade VALUES (1000, 'LISBOA')"); query.exec();
    4. query.prepare("INSERT INTO Localidade VALUES (1049, 'LISBOA')"); query.exec();
    5. query.prepare("INSERT INTO Localidade VALUES (1050, 'LISBOA')"); query.exec();
    6. query.prepare("INSERT INTO Localidade VALUES (2000, 'PÓVOA DE SANTARÉM')"); query.exec();
    7. query.prepare("INSERT INTO Localidade VALUES (2000, 'SANTARÉM')"); query.exec();
    8. query.prepare("INSERT INTO Localidade VALUES (2000, 'SÃO VICENTE DO PAUL')"); query.exec();
    9. query.prepare("INSERT INTO Localidade VALUES (2005, 'AZOIA DE BAIXO')"); query.exec();
    10. query.prepare("INSERT INTO Localidade VALUES (2005, 'PÓVOA DA ISENTA')"); query.exec();
    11. query.prepare("INSERT INTO Localidade VALUES (2025, 'ABRÃ')"); query.exec();
    12. query.prepare("INSERT INTO Localidade VALUES (2025, 'GANÇARIA')"); query.exec();
    13. query.prepare("INSERT INTO Localidade VALUES (2025, 'TREMÊS')"); query.exec();
    14.  
    15. query.prepare("INSERT INTO Localidade VALUES (2090, 'ALPIARÇA')"); query.exec();
    To copy to clipboard, switch view to plain text mode 
    ... fails to be displayed correctly.

    Guess i could i write this lines to force the second column to be an UTF8 ... ?
    Last edited by graciano; 24th May 2010 at 13:15.

  4. #4
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Chowing Portuguese characters in a QTableView

    Well ... this only worked for me by creating a function to read the text from a csv file and adding the data to the model:
    Qt Code:
    1. void LocalidadeDialog::inserirLocalidadesNacionais()
    2. {
    3. QFile ficheiro(":/ctt/ctt");
    4. if (!ficheiro.open(QIODevice::ReadOnly | QIODevice::Text))
    5. return;
    6. QTextStream in(&ficheiro);
    7. QString linha;
    8. QString col1, col2;
    9. int separador;
    10. QSqlRecord registo;
    11. QSqlField c1("idLocalidade", QVariant::Int);
    12. QSqlField c2("nomeLocalidade", QVariant::String);
    13. registo.append(c1);
    14. registo.append(c2);
    15. while (!in.atEnd())
    16. {
    17. linha = in.readLine();
    18. separador = linha.indexOf(",");
    19. col1 = linha.mid(0, separador);
    20. col2 = linha.mid(separador+ 1);
    21. registo.setValue("idLocalidade", col1);
    22. registo.setValue("nomeLocalidade", col2);
    23. modeloLocalidade->insertRecord(-1, registo);
    24. modeloLocalidade->select(); // OK
    25. }
    26. //modeloLocalidade->select(); //not OK
    27. }
    To copy to clipboard, switch view to plain text mode 

    Why is that line 24 works and if i do the same at line 26 (outside the loop) i get lots of blank lines if the insert fails (duplicate keys in the database)?

    By the way ... what showld i use to create some "entertainment" while the data is being inserted into the database, assuming that i want no interaction user/application?

    Thanks

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Chowing Portuguese characters in a QTableView

    Hi,

    you set utf8 global but in your table Localidade you use latin1 as character set. You might want change that. Further make sure your cpp file is saved as utf8! That is possible the reason why inserting work with a file and not in your code. Also try to use QString::fromUtf8() for your inserts.

    You also can set the default character set after establishing the connection to the database with:
    sql Code:
    1. SET NAMES 'utf8';
    2. SET CHARACTER SET 'utf8';
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lykurg for this useful post:

    graciano (25th May 2010)

  7. #6
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Chowing Portuguese characters in a QTableView

    Quote Originally Posted by Lykurg View Post
    Hi,

    you set utf8 global but in your table Localidade you use latin1 as character set. You might want change that.
    I had noticed that just afted i posted it

    You might just be right about saving the file in utf8.

    Any way ... it was not a good idea to make the insertion depend on my cpp!

    About the other subject ... a progress bar?!

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Chowing Portuguese characters in a QTableView

    Quote Originally Posted by graciano View Post
    About the other subject ... a progress bar?!
    why not. But how long will it take? If it's only a couple of seconds, I would use a simple message that things currently imported. And the common "circling dot's" for indicating a process.

  9. The following user says thank you to Lykurg for this useful post:

    graciano (25th May 2010)

Similar Threads

  1. QString with escape characters
    By Jeffb in forum Newbie
    Replies: 1
    Last Post: 26th April 2010, 14:06
  2. Invisible characters
    By Myrgy in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2010, 12:37
  3. QTableView + sorting + numbers & special characters
    By foggy-mind in forum Qt Programming
    Replies: 5
    Last Post: 4th June 2009, 11:11
  4. special characters in xml
    By khcbabu in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 22:10
  5. encode characters
    By smarinr in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 06:46

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.