Help your plugin creation.
Gives an usable template, and an empty UI.
This is your first step to your first plugin.
You don't need anymore to reboot Qgis when you make a modification in your script.
main file.py
interface.py
resources.py
interface.ui
resources.qrc
pyuic4
pyrcc4
Very simplified schema
Open Qt Designer
Load empty UI generated with Plugin Builder and add classes :
Class | Use for |
---|---|
QPushButton | Clic (run script, load file, save...) |
QLabel | Text or image |
QgsMapLayerComboBox | List of all raster open in Qgis |
QComboBox | List fields you want |
... | ... |
Once your ui is done :
Download PyQt4 (binary packages):
https://www.riverbankcomputing.com/software/pyqt/download
If you used plugin builder, change call for your ui (cf my post)
Open your main file.py (ie my_first_plugin.py)
In import PyQt4.QtGui add QDialog and QFileDialog :
QFileDialog is loading/saving window
QDialog is to know for where the action comes from
Add QDialog in your main class dependency, then init QDialog:
from PyQt4.QtGui import QDialog, QFileDialog
myfirstplugin( QDialog ) :
QDialog.__init__(self)
Import QgsMessageLog for a perfect debugging
Then you create a log message with :
from qgis.core import QgsMessageLog
QgsMessageLog.logMessage('bug using : '+yourVariabletoTest)
Once QDialog is init, you can use self.sender()
It gives you the origin of an action because you can't use args
Example : you clic on the "runButton" and self.dlg is your ui() :
self.dlg.runButton.clicked.connect(self.saveTo)
def saveTo(self):
if self.sender() == self.dlg.runButton:
#do the job
fileName = QFileDialog.getSaveFileName(self.dlg, \
"Select output file","","TIF (*.tif)")
Then you can open a window that let the user select the file to save :
Now, what to do with your filename ?
Save it in a QLineEdit field.
If your file outRaster is a QLineEdit field, you can show to the user his file choice :
self.dlg.outRaster.setText(fileName)
outRasterVariable = self.dlg.outRaster.text()
Then you can assign it to a variable
You have now all basics to make your own Qgis plugin.
Some tips :
try:
from osgeo import gdal
except:
QgsMessageLog.logMessage('Gdal is not available')
Remember to use QgsMessageLog to debbug
Make sure your script runs in python first, then merge it in your plugin
Use self.sender() from QDialog to replace args from ui
To avoid bugs, use try/except function