3

I'm building a QGIS plugin which only requires user interaction to select a save location for an output file. I used Plugin Builder to create a template and assumed I could simply replace the template dialog with a QFileDialog object. Here's the relevant code:

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

    # Create the dialog with elements (after translation) and keep reference
    # Only create GUI ONCE in callback, so that it will only load when the plugin is started
    if self.first_start == True:
        self.first_start = False
        self.dlg = QFileDialog(self.iface)
        self.dlg.setFileMode(QFileDialog.AnyFile)
        self.dlg.setNameFilter(self.tr("Zip files (*.zip)"))
        # self.dlg = GiftWrapDialog()

    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
    # See if OK was pressed
    if result:
        # Do something useful here - delete the line containing pass and
        # substitute with your code.
        pass

This code throws an error when I try to run the plugin:

TypeError: arguments did not match any overloaded call:   QFileDialog(QWidget, Union[Qt.WindowFlags, Qt.WindowType]): argument 1 has unexpected type 'QgisInterface'   QFileDialog(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = ''): argument 1 has unexpected type 'QgisInterface' 

Is there any way for my plugin to simply show a standard file dialog without having to create my own dialog?

1
  • You can do that if you choose processing provider when building plugin using plugin builder.
    – Mat
    Mar 7, 2020 at 22:13

1 Answer 1

2

There may be different types of QFileDialog in a QGIS plugin and it is preferable do not put it in run method. In the case of 'getSaveFileName' QFileDialog type, you have a following code snippet of one of my plugins (built with Plugin Builder). The 'select_output_file' function was placed before run method and it is connected to a QPushButton associated to QFileDialog (for placing file name there is also associated a QLineEdit object).

.
.
.
from PyQt5.QtWidgets import QAction, QFileDialog
.
.
.
    def add_action(
        self,
        icon_path,
        text,
        callback,
        enabled_flag=True,
        add_to_menu=True,
        add_to_toolbar=True,
        status_tip=None,
        whats_this=None,
        parent=None):
.
.
.
        self.dlg.lineEdit.clear()
        self.dlg.pushButton.clicked.connect(self.select_output_file)

        okBtn = self.dlg.okButton
        okBtn.clicked.connect(self.input_values)

        return action
.
.
.
    def select_output_file(self):

        filename = QFileDialog.getSaveFileName(self.dlg, "Select output file ","", '*.tif')
        ext = ".tif"
        ext_filename = filename[0][-4:]

        if ext_filename != ext:
            filename[0] += ext 

        self.dlg.lineEdit.setText(filename[0])
.
.
.

In following code snippet you have an example of 'getExistingDirectory' QFileDialog where I also used Plugin Builder to create a template.

.
.
.
from PyQt5.QtGui import QAction, QIcon, QTableWidgetItem, QFileDialog
.
.
.
    def add_action(
.
.
.
        self.dlg.lineEdit.clear()
        self.dlg.pushButton.clicked.connect(self.select_output_directory)
.
.
.
    def select_output_directory(self):

        folder = str(QFileDialog.getExistingDirectory(self.dlg, "Select Directory"))

        self.dlg.lineEdit.setText(folder)
.
.
.

Your Answer

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

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