2018-02-06
Some hints for porting QGIS 2 plugins to the new API of QGIS 3.
2to3 path/to/QGIS/scripts/2to3 /path/to/your/plugin; -w will write the changes while creating .bak files for backupgrep -rl 'path_to_QGIS3_plugins/yourplugin/' -e 'PyQt4\.' | grep "\.bak" -v | grep "cache" -v | xargs sed -i 's/PyQt4/PyQt5/g'-r = recursive-l = list files1st pipe = don't include the bak files generated by 2to32nd pipe = don't include cache3rd pipe = take files from grep and replace PyQt4 with PyQt5pyrcc5
pyrcc5 -o resources_rc.py resources.qrcpyuic5
find . -name \*.ui -type f | xargs basename -s .ui | xargs -I {} pyuic5 -o ./view/Ui_{}.py ./view/{}.ui --from-imports--from-imports argument to make a proper import (QGIS does not seem to handle a simple import resources_rc too well)QtWidgets import to .py files containing QDialog (other classes could be affected, too; like QWidget or QMessagebox)
grep -rl '.' -e 'QDialog' | grep "\.bak" -v | grep "cache" -v | grep "\.ui" -v | grep "Ui_" -v | xargs sed -i '3i from PyQt5.QtWidgets import *'QDialog; do not handle “.bak” files; do not handle “cache” files; do not handle “.ui” files; do not handle “Ui_…” files; insert import in 3rd line (because 2to3.py likely introduced two “future” imports at the top)QgsDataSourceURI became QgsDataSourceUriQgsLegendInterface with its currentLayer() is no more, instead QgsMapLayerComboBox is usedQGis::WkbType have been changed to use QgsWkbTypes::Type; so for example, Qgis.Point becomes QgsWkbTypes.Point (QGIS core library import is needed)QgsRubberBand expects QgsWkbTypes::GeometryType for reset() (instead of Qgis.Point)QgsMapLayerRegistry becomes QgsProjectselectedFeaturesIds() replaced by selectedFeatureIds()