[Qgis-user] newbie QGIS, pyQT and python question

Ricardo Filipe Soares Garcia da ricardo.garcia.silva at gmail.com
Sat Apr 2 05:35:43 PDT 2011


Hi Karsten
I think these questions would be better suited for the qgis-developer
mailing-list.
Looking at pyqt's documentation[1], it seems the getOpenfileName
method's, first argument is the parent widget, and must be a QWidget
(or one of its subclasses) or None.
So you have some options:
1 - make this dialog a child of your main plugin window (which is not
present on the code snippet in your message).
2 - make this dialog a child of the main qgis window:

# code example
QgisMainWindow = qgis.utils.iface.mainWindow()
filename = QFileDialog.getOpenFileName(QgisMainWindow, 'Open File', '.')

3 - leave the 'parent' argument as None:

# code example
filename = QFileDialog.getOpenFileName(None, 'Open File', '.')


I think either option should work. Good luck with your plugin ;)

[1] - http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qfiledialog.html



On Sat, Apr 2, 2011 at 2:40 AM, karsten vennemann <karsten at terragis.net> wrote:
>
> Hi,
>
> I am working on my first QGIS plug-in wohoo .
> But honestly this is my first ever stab at OO code in python using pyQT. I have http://pastebin.com/dWT5eezP (for code highlighting - or see end of message below trace back)
> The line
> self.inputConfig()
> throws his error : argument 1 has unexpected type 'instance' .See traceback below. There I am trying to open a file selection dialogue window ....
>
> Ay idea what it excepts instead of self ? Any python gurus ?
>
> Thanks
> Karsten
>
> ________________________________________________________________________________________________
>
> Traceback (most recent call last):
>   File "D:/OSGeo4W/apps/qgis/./python/plugins\StreamEditCustomForm\streameditcustomform.py", line 72, in run
>     self.inputConfig()
>   File "D:/OSGeo4W/apps/qgis/./python/plugins\StreamEditCustomForm\streameditcustomform.py", line 54, in inputConfig
>     filename = QFileDialog.getOpenFileName(self, 'Open File', '.')
> TypeError: QFileDialog.getOpenFileName(QWidget parent=None, QString caption=QString(), QString directory=QString(), QString filter=QString(), QString selectedFilter=None, QFileDialog.Options options=0): argument 1 has unexpected type 'instance'
>
> Python version:
> 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)]
> ________________________________________________________________________________________________
>
> # Import the PyQt and QGIS libraries
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> from qgis.core import *
> # Initialize Qt resources from file resources.py
> import resources
> # Import the code for the dialog
> from streameditcustomformdialog import StreamEditCustomFormDialog
>
> class StreamEditCustomForm:
>
>     def __init__(self, iface):
>         # Save reference to the QGIS interface
>         self.iface = iface
>
>     def initGui(self):
>         # Create action that will start plugin configuration
>         self.action = QAction(QIcon(":/plugins/streameditcustomform/icon.png"), \
>             "Stream Edit Custom Form", self.iface.mainWindow())
>         # connect the action to the run method
>         QObject.connect(self.action, SIGNAL("triggered()"), self.run)
>
>         # Add toolbar button and menu item
>         self.iface.addToolBarIcon(self.action)
>         self.iface.addPluginToMenu("&Stream Edit Custom Form", self.action)
>
>     def unload(self):
>         # Remove the plugin menu item and icon
>         self.iface.removePluginMenu("&Stream Edit Custom Form",self.action)
>         self.iface.removeToolBarIcon(self.action)
>
>
>     def inputConfig(self):
>             filename = QFileDialog.getOpenFileName(self, 'Open File', '.')
>             fname = open(filename)
>             data = fname.read()
>             self.textEdit.setText(data)
>             fname.close()
>
>     # run method that performs all the real work
>     def run(self):
>
>         # create and show the dialog
>         dlg = StreamEditCustomFormDialog()
>         # show the dialog
>         dlg.show()
>         result = dlg.exec_()
>         # See if OK was pressed
>         if result == 1:
>             # do something useful (delete the line containing pass and
>             # substitute with your code
>             self.inputConfig()
>             pass
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-user
>



--
___________________________ ___ __
Ricardo Garcia Silva



More information about the Qgis-user mailing list