proski
proski

Reputation: 3929

python3-qt4 discards non-ascii characters from unicode strings in QApplication contructor

I'm trying to port a PyQt based application to Python 3. If I initialize QApplication with sys.argv, QApplication.arguments() returns those arguments without non-ascii characters. The workaround is to encode arguments as bytes. But it's hard for me to believe that it's the best approach. Why cannot PyQt just accept unicode strings? Maybe some locale settings need to be tweaked?

Example code:

#! /usr/bin/python3

from PyQt4.QtGui import QApplication

args = ["test", "tsch\u00fcss", "tsch\u00fcss".encode("utf-8")]
print("args:", args)
qapp = QApplication(args)
qargs = qapp.arguments()
print("qargs:", qargs)

Output:

args: ['test', 'tschüss', b'tsch\xc3\xbcss']
qargs: ['test', 'tschss', 'tschüss']

Things that make no difference:

import sip
sip.setapi("QString", 2)

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

Ubuntu 14.10, python3-pyqt4 4.11.2+dfsg-1

Upvotes: 2

Views: 244

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

This looks like a bug in PyQt, as it is handled correctly by PySide.

And there seems no reason why the args can't be properly encoded before being passed to the application constructor:

>>> import os
>>> from PyQt4 import QtCore
>>> args = ['føø', 'bær']
>>> app = QtCore.QCoreApplication([os.fsencode(arg) for arg in args])
>>> app.arguments()
['føø', 'bær']

If you want to see this get fixed, please report it on the PyQt Mailing List.

Upvotes: 2

Related Questions