PDA

View Full Version : Printing non-ascii characters.



kikapu
24th June 2007, 18:06
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 ??:confused:
Is there something in Qt that i am not doing (or at all...) correcltly ?


Code is below:


# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtCore, QtSql, QtGui

app = QtGui.QApplication(sys.argv)
db = QtSql.QSqlDatabase.addDatabase("QODBC");
db.setDatabaseName("pm");
f = db.open()

if f:
print "Connected"
query = QtSql.QSqlQuery(db)
query.prepare("{call graduation_fetch(?)}")
query.setForwardOnly(True)
query.bindValue(0, QtCore.QVariant(4), QtSql.QSql.In)
query.exec_()

while query.next():
candidate = QtCore.QVariant.toString(query.value(0))
print QtCore.QString.toUtf8(candidate)

db.close()

and of course, thanks for any help!

jacek
24th June 2007, 18:49
while query.next():
candidate = QtCore.QVariant.toString(query.value(0))
print QtCore.QString.toUtf8(candidate)
It should be:
while query.next():
candidate = query.value(0).toString()
print candidate.toUtf8()

but those strings will be displayed correctly only if your console uses UTF-8. You might try toAscii() or toLocal8Bit() instead.

kikapu
24th June 2007, 19:26
It should be:
while query.next():
candidate = query.value(0).toString()
print candidate.toUtf8()


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! :)