Skip to content

Instantly share code, notes, and snippets.

@m-kuhn
Forked from NathanW2/pyqgis-sip2.rst
Last active December 18, 2015 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-kuhn/5746815 to your computer and use it in GitHub Desktop.
Save m-kuhn/5746815 to your computer and use it in GitHub Desktop.
Minor updates.

The QVariant type doesn't exist 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()
toFloat()
toStringList()
toByteArray()
QVariant(..)
QString(...)

Caveat: Pay attention to convert types manually where appropriate. If you had a field containing an integer before and require it as a float/string representation in your plugin convert it with float(myvar) or unicode(myvar) where you had myvar.toFloat() / myvar.toString() before.

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 unicode. 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment