Results 1 to 3 of 3

Thread: Printing non-ascii characters.

  1. #1
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Printing non-ascii characters.

    Hi,

    after a quest for connecting to an odbc data source and after much documentation reading, i finally have a snippet that i can connect and execute a parameterized stored procedure!

    The sp is a simple one, getting an integer and returning one colymn of string data

    The only 2 questions i have is that if i HAVE to declare the stored procedure parameter type as QVariant(4) or is another way to do this,
    and (more important) how i can print non-ascii data that exists in the result set...

    I am using Eclipse/PyDev (if that matters) but i print garbage from the snipet below,
    and am not sure if the i correctly coded the part about QVariant.toString and QtCore.QString.toUtf8(candidate). (i am propably not...)

    In a real app would put the data in a grid but what about now ?? I check the variable"candidate" in the debugger and it correctly contains the Greek characters that came from the database, but how i can...print them ??
    Is there something in Qt that i am not doing (or at all...) correcltly ?


    Code is below:

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. import sys
    4. from PyQt4 import QtCore, QtSql, QtGui
    5.  
    6. app = QtGui.QApplication(sys.argv)
    7. db = QtSql.QSqlDatabase.addDatabase("QODBC");
    8. db.setDatabaseName("pm");
    9. f = db.open()
    10.  
    11. if f:
    12. print "Connected"
    13. query = QtSql.QSqlQuery(db)
    14. query.prepare("{call graduation_fetch(?)}")
    15. query.setForwardOnly(True)
    16. query.bindValue(0, QtCore.QVariant(4), QtSql.QSql.In)
    17. query.exec_()
    18.  
    19. while query.next():
    20. candidate = QtCore.QVariant.toString(query.value(0))
    21. print QtCore.QString.toUtf8(candidate)
    22.  
    23. db.close()
    To copy to clipboard, switch view to plain text mode 

    and of course, thanks for any help!
    Last edited by kikapu; 24th June 2007 at 18:19.
    Software Engineer, riding a Kona King Kikapu 2007

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing non-ascii characters.

    Quote Originally Posted by kikapu View Post
    while query.next():
    candidate = QtCore.QVariant.toString(query.value(0))
    print QtCore.QString.toUtf8(candidate)
    It should be:
    Python Code:
    1. while query.next():
    2. candidate = query.value(0).toString()
    3. print candidate.toUtf8()
    To copy to clipboard, switch view to plain text mode 
    but those strings will be displayed correctly only if your console uses UTF-8. You might try toAscii() or toLocal8Bit() instead.

  3. The following user says thank you to jacek for this useful post:

    kikapu (24th June 2007)

  4. #3
    Join Date
    May 2007
    Posts
    23
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Printing non-ascii characters.

    Quote Originally Posted by jacek View Post
    It should be:
    Python Code:
    1. while query.next():
    2. candidate = query.value(0).toString()
    3. print candidate.toUtf8()
    To copy to clipboard, switch view to plain text mode 
    Hmm...so, i correctly did not like that QVariant stuff, but i use Python as you already know so the back-and-forth of the C++ documentations is not every time so straightforward


    but those strings will be displayed correctly only if your console uses UTF-8. You might try toAscii() or toLocal8Bit() instead.
    Thanks a lot! That toLocal8Bit() did the trick! The Greek names are now correctly displayed to the Console Window.

    So, i already did a form by using the Qt Designer and i can display the same data to a listWidget.

    Thanks again!
    Software Engineer, riding a Kona King Kikapu 2007

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18

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.