1

I'm quite new to the plugin development. So I'm trying just to use the clip method to clip specific layers. But after the reloading the plugin I'm always getting thid message:

ImportError: cannot import name 'QFileDialog' from 'qgis.PyQt' (C:\OSGeo4W/apps/qgis-ltr/./python\qgis\PyQt\__init__.py) 

Traceback (most recent call last):
  File "C:\OSGeo4W/apps/qgis-ltr/./python\qgis\utils.py", line 423, in _startPlugin
    plugins[packageName] = package.classFactory(iface)
  File "C:\Users/tomas/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\de\__init__.py", line 35, in classFactory
    from .de import de
  File "C:\OSGeo4W/apps/qgis-ltr/./python\qgis\utils.py", line 888, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:\Users/tomas/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\de\de.py", line 31, in 
    from .de_dialog import deDialog
  File "C:\OSGeo4W/apps/qgis-ltr/./python\qgis\utils.py", line 888, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:\Users/tomas/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\de\de_dialog.py", line 28, in 
    from qgis.PyQt import QtWidgets, QFileDialog, QMessageBox
ImportError: cannot import name 'QFileDialog' from 'qgis.PyQt' (C:\OSGeo4W/apps/qgis-ltr/./python\qgis\PyQt\__init__.py)

This is my code from my plugin dialog that is called de:

import os

from qgis.PyQt import uic
from qgis.PyQt import QtWidgets, QFileDialog, QMessageBox
import processing
from qgis.core import QgsVectorLayer

# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'de_dialog_base.ui'))


class deDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(deDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.setupUi() you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.pushButton.clicked.connect(self.print_hello)

    def print_hello(self):
        # let the user select input, output, and clip files using a file dialog
        input_file, _ = QFileDialog.getOpenFileName(None, "Select input file", "", "Shapefile (*.shp)")
        clip_file, _ = QFileDialog.getOpenFileName(None, "Select clip file", "", "Shapefile (*.shp)")
        output_file, _ = QFileDialog.getSaveFileName(None, "Select output file", "", "Shapefile (*.shp)")

        # exit if any of the file paths are empty
        if not input_file or not clip_file or not output_file:
            QMessageBox.critical(None, "Error", "You must choose all three files.")
            return

        # load input and clip layers
        input_layer = QgsVectorLayer(input_file, "input", "ogr")
        clip_layer = QgsVectorLayer(clip_file, "clip", "ogr")

        # set the clip parameters
        params = {
            "INPUT": input_layer,
            "OVERLAY": clip_layer,
            "OUTPUT": output_file,
            "VALIDITY_CHECK": False,
            "INVALID_OUTPUT": "memory:",
            "CLIP": True,
            "OUTPUT": output_file,
            "OUTPUT_LAYER": "output",
            "INVALID_OUTPUT_LAYER": "invalid",
            "INVALID_FEATURES": "memory:",
            "IGNORE_INVALID": True,
        }

        # run the clip algorithm
        processing.run("native:clip", params)

        # reload the output layer
        output_layer = QgsVectorLayer(output_file, "output", "ogr")

        # check if the output layer was created successfully
        if not output_layer.isValid():
            print("Error: Output layer could not be created")
        else:
            print("Output layer created successfully")

What can I do to fix?

QGIS verze 3.28.4-Firenze, Qt verze 5.15.3, Verze Pythonu 3.9.5, Verze GDAL/OGR 3.6.3, Verze PROJ 9.2.0, EPSG Registry database version () GEOS verze 3.11.2-CAPI-1.17.2, SQLite verze 3.41.1, PDAL verze 2.5.2, PostgreSQL client version unknown, SpatiaLite version 5.0.1, QWT verze 6.1.6, QScintilla2 verze 2.13.1, OS verze Windows 10 Version 2009.

2
  • The module is PyQt5.QtWidgets: from PyQt5.QtWidgets import QFileDialog
    – RainForest
    Commented Mar 25, 2023 at 14:06
  • Thank you now it works! Commented Mar 25, 2023 at 21:25

1 Answer 1

3

QFileDialog and QMessageBox are in the QtWidgets module. So use:

from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
0

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.