3

I have created a QGIS plugin to import a tab separated value file, using:

filename = QFileDialog.getOpenFileName(self.dlg, "Select file to import ", path, '*.*')
uri="file:///"+filename+"?"+"delimiter=%s&xField=%s&yField=%s"%("\t", "Set Grid Easting", "Set Grid Northing")
vlayer = QgsVectorLayer(uri, "SpreadTests", "delimitedtext")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

It works as expected except that the dialog box to select the file stays open after a selection is made. Is there something I am doing wrong? or something else I need to add?

  • Sure it won't be the issue, but looking at my own plugin's use of QFileDialog, I use self as the first argument, not `self.dlg' (or equivalent). – Tom Chadwin Oct 9 '15 at 14:58
  • I just tried it without the ".dlg" but get an error as soon as I initiate the plugin, so is there any other difference? – AlPhuket Oct 9 '15 at 15:55
  • whereabouts is this code in your plugin? Is it in initGui() or __init__()? Or is it in a method which you've connected to a signal, e.g. when the user clicks a button? I find the latter approach works for me. – Steven Kay Oct 9 '15 at 21:07
  • Thanks, Steven. It looks like you were on the right track, but I got another answer before I could reply to your suggestion. – AlPhuket Oct 11 '15 at 15:57
3

You are mixing the slot function code with the code necessary for a result. That is the problem.

For example, I created one arbitrary plugin to 'open files'. Next snipped code, at __init__ method, is for the button to connect with the slot where is the QFileDialog:

.
.
.
    def __init__(self, iface):
        """Constructor.
.
.
.
        #QFileDialog for open file
        self.dlg.lineEdit.clear()
        self.dlg.pushButton.clicked.connect(self.select_open_file)
.
.
.

For convention, the slot function is placed before run method:

.
.
.
    def select_open_file(self):

        filename = QFileDialog.getOpenFileName(self.dlg, "Select file to import ", "", '*.*')

        self.dlg.lineEdit.setText(filename)


    def run(self):
        """Run method that performs all the real work"""
.
.
.

if result:

.
.
.
        if result:
            # Do something useful here - delete the line containing pass and
            # substitute with your code.

            filename = self.dlg.lineEdit.text()

            print filename

The "something useful here" is to print open_file but, in your case, you have to put the code.

uri="file:///"+filename+"?"+"delimiter=%s&xField=%s&yField=%s"%("\t", "Set Grid Easting", "Set Grid Northing")
vlayer = QgsVectorLayer(uri, "SpreadTests", "delimitedtext")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

It works for me as you can see at the next images:

Selecting the input file:

enter image description here

After click in 'Open', the QFileDialog was closed and the path of the shapefile was added to the QLineEdit:

enter image description here

After click in OK, the name of filename was printed at the Python Console.

enter image description here

  • Many thanks - I'll have to look up and learn about "slot function code", but when I changed my code as suggested, it's all good now. – AlPhuket Oct 11 '15 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.