Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Last active December 18, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NathanW2/5734767 to your computer and use it in GitHub Desktop.
Save NathanW2/5734767 to your computer and use it in GitHub Desktop.
A conversion guide for the new QGIS API

The QVariant type doesn't exsits any more so any methods returning QVariant will be auto converted to Python types. You no longer need to convert the return type using the toXXXX methods.

Remove all:

toString()
toList()
toInt()
toStringList()
toByteArray()
QVariant(..)
QString(...)

Set QSettings return type

More info: http://pyqt.sourceforge.net/Docs/PyQt4/pyqt_qsettings.html

Before:

settings.value(“/yourboolsetting”, True).toBool()
settings.value(“/yourintsetting”, 10).toInt()[0]
settings.value(“/yourintsetting”).toByteArray()

After:

settings.value(“/yourboolsetting”, True, type=bool)
settings.value(“/yourintsetting”, 10, type=int)
settings.value(“/yourintsetting”, QByteArray(), type=QByteArray)

Replace QString methods

QString no longer exits in the new QGIS API. Any methods that return a QString will be converted into a native Python str. All QString methods need to be replaced with native string methods.

Before:

yourstring.right(4)
files.join(",")
if yourstring.length() > 4:
if yourstring.isEmpty()

After:

yourstring[4:]
",".join(files)
if len(yourstring) > 4
if not yourstring

Replace QStringList with list

Before:

mystrings = QStringList()

After:

mystrings = []

Remove QVariant calls

The QVariant also doesn't exsits any more so any methods returning QVariant will be auto converted to Python types. However QVariant can still be used to access it's emun values e.g. QVariant.Int can set be used.

Before:

myvalue = QVariant(10)
myvalue = QVariant("Hello World")

After:

myvalue = 10
myvalue = "Hello World"

Replace signals with new style signals and connections

Before:

self.emit(SIGNAL("valuesChanged(const QStringList &)"), self.getArguments())

After:

class Test():
  valuesChagned = QtCore.pyqtSignal(list)

  def yourmethod():
    self.valuesChagned.emit(self.getArguments)
@ccrook
Copy link

ccrook commented Jun 8, 2013

Thanks Nathan - much cleaner. How does unicode get handled with the conversion of QString?

@NathanW2
Copy link
Author

NathanW2 commented Jun 9, 2013

Chris, I think in Python 2.7 it will QString == str but in Python 3 QString == unicode. So for QGIS you need to do unicode(lineedit.text()) if you want unicode strings.

@mlaloux
Copy link

mlaloux commented Jun 9, 2013

I have answered in the QGIS user list and this is very good news indeed: the end of the PyQt "pollution" in the data processing. Thank you very much Nathan

@borysiasty
Copy link

@NathanW2: isn't it opposite? In Python 2.x QString is directly mapped to unicode. In 3.0 I guess it's not necessary so it stays in the "plain" str.

@NathanW2
Copy link
Author

NathanW2 commented Jun 9, 2013

I think you are right.

For example, an application can choose whether QString is implemented as a Python type, or is automatically converted to and from a Python v2 unicode object or a Python v3 string object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment