Differences between revisions 1 and 2
Revision 1 as of 2010-10-21 15:41:37
Size: 1226
Comment: List a common problem with solution porting pyqt apps to python3
Revision 2 as of 2010-10-22 17:13:56
Size: 904
Comment:
Deletions are marked like this. Additions are marked like this.
Line 5: Line 5:
One method of porting is to replace all occurrences of QString with ordinary python strings. To keep your code similar on both python3 and python2, you probably want to pick either the old or the new API and then have your code explicitly use that like so:
Line 7: Line 7:
If you want code that is the same for Python 2.x and Python 3.x there's a few options to try. You can continue using QString if you use the following code: To pick the old API:
Line 9: Line 9:
try:
    from PyQt4.QtCore import QString
except ImportError:
    # we are using Python3 so QString is not defined
    QString = type("")
import sip
sip.setapi('QString', 1)

from PyQt4 import QtCore

# This works as the old API has QString
print(QtCore.QString)
Line 16: Line 18:
You can also set your code to use API2 even when running on python2 and then port all of your code to API2. To use API2 everywhere:
Line 23: Line 25:
# Now QtCore.QString is no longer available -- use python2's unicode type instead # Now QtCore.QString is no longer available -- use python2's unicode type or python3's str type instead

PyQt 4.6+

PyQt-4.6 and greater have a new API called API2 that is the default when run on Python-3.x. The major difference is that QString does not exist in API2; the library uses the native Python3 str type instead.

To keep your code similar on both python3 and python2, you probably want to pick either the old or the new API and then have your code explicitly use that like so:

To pick the old API:

import sip
sip.setapi('QString', 1)

from PyQt4 import QtCore

# This works as the old API has QString
print(QtCore.QString)

To use API2 everywhere:

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

from PyQt4 import QtCore

# Now QtCore.QString is no longer available -- use python2's unicode type or python3's str type instead

From the pyqt4 reference manual

PortingPythonToPy3k/PyQt4 (last edited 2010-10-22 17:13:56 by ToshioKuratomi)

Unable to edit the page? See the FrontPage for instructions.