2

I am writing a plugin that offers to open a CSV. I know the local path to the CSV but I don't know if it contains spatial data nor which name the X and Y could have.

The best way I can think of, is to utilize the QGIS Delimited Text Widget and set the path to the file, so it is possible to choose X and Y (if existing) from the native dialog (and use the other functions if needed).

My only problem is, that I can't find a way to set the path to the file inside the widget. Is there a way I can do this?

The code to open the widget:

delimited_dlg = QgsProviderRegistry.instance().selectWidget("delimitedtext", self.main_win)  # use iface.mainWindow() in console

QObject.connect(delimited_dlg, SIGNAL( "addVectorLayer( QString, QString, QString )" ),
           self.main_win, SLOT( "addSelectedVectorLayer( QString, QString, QString )" ) )

delimited_dlg.show()

The native Class QgsDelimitedTextSourceSelect uses the variable txtFilePath to store the path and the Class QgsDelimitedTextProvider takes the const QString& uri as argument. Is there any chance I could set those?

1 Answer 1

2

Not the neatest way of doing things, but you can achieve what you want by running this code into the QGIS Python console:

delimited_dlg = QgsProviderRegistry.instance().selectWidget("delimitedtext", iface.mainWindow())
delimited_dlg.children()[1].children()[2].setText("/path/to/your/csv_file.csv")
delimited_dlg.show()

delimited_dlg is the dialog, which contains children. Second child is a QWidget, which in turns contains some QLabels and QLineEdits, among other widgets. The QLineEdit widget you want is the third widget of the aforementioned QWidget, and we know we can set a QLineEdit using setText(), so we use it.

That's it!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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